summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/mre/function_handle_unittest.py
blob: d3bf71dfe666ff928401d42c5fa85afab741bf66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from tracing.mre import function_handle


class ModuleToLoadTests(unittest.TestCase):

  def testExactlyOneHrefOrFilename(self):
    with self.assertRaises(Exception):
      function_handle.ModuleToLoad()

    with self.assertRaises(Exception):
      function_handle.ModuleToLoad('foo', 'foo')

  def testRepr(self):
    mtl0 = function_handle.ModuleToLoad(href='/foo')
    mtl1 = function_handle.ModuleToLoad(filename='foo.html')

    self.assertEquals(str(mtl0), 'ModuleToLoad(href="/foo")')
    self.assertEquals(str(mtl1), 'ModuleToLoad(filename="foo.html")')

  def testAsDict(self):
    mtl0 = function_handle.ModuleToLoad(href='/foo')
    mtl1 = function_handle.ModuleToLoad(filename='foo.html')

    self.assertEquals(mtl0.AsDict(), {
        'href': '/foo'
    })

    self.assertEquals(mtl1.AsDict(), {
        'filename': 'foo.html'
    })

  def testFromDict(self):
    module_dict0 = {
        'href': '/foo'
    }

    module_dict1 = {
        'filename': 'foo.html'
    }

    mtl0 = function_handle.ModuleToLoad.FromDict(module_dict0)
    mtl1 = function_handle.ModuleToLoad.FromDict(module_dict1)

    self.assertEquals(mtl0.href, '/foo')
    self.assertIsNone(mtl0.filename)
    self.assertEquals(mtl1.filename, 'foo.html')
    self.assertIsNone(mtl1.href)


class FunctionHandleTests(unittest.TestCase):

  def testRepr(self):
    module = function_handle.ModuleToLoad(href='/foo')
    handle = function_handle.FunctionHandle([module], 'Bar')

    self.assertEquals(
        str(handle),
        'FunctionHandle(modules_to_load=[ModuleToLoad(href="/foo")], '
        'function_name="Bar")')

  def testAsDict(self):
    module = function_handle.ModuleToLoad(href='/foo')
    handle = function_handle.FunctionHandle([module], 'Bar')

    self.assertEquals(
        handle.AsDict(), {
            'modules_to_load': [{'href': '/foo'}],
            'function_name': 'Bar'
        })

  def testFromDict(self):
    handle_dict = {
        'modules_to_load': [{'href': '/foo'}],
        'function_name': 'Bar'
    }

    handle = function_handle.FunctionHandle.FromDict(handle_dict)
    self.assertEquals(len(handle.modules_to_load), 1)
    self.assertEquals(handle.modules_to_load[0].href, '/foo')
    self.assertEquals(handle.function_name, 'Bar')