• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for anno module."""
16
17import ast
18
19from tensorflow.python.autograph.pyct import anno
20from tensorflow.python.platform import test
21
22
23# TODO(mdan): Consider strong types instead of primitives.
24
25
26class AnnoTest(test.TestCase):
27
28  def test_basic(self):
29    node = ast.Name()
30
31    self.assertEqual(anno.keys(node), set())
32    self.assertFalse(anno.hasanno(node, 'foo'))
33    with self.assertRaises(AttributeError):
34      anno.getanno(node, 'foo')
35
36    anno.setanno(node, 'foo', 3)
37
38    self.assertEqual(anno.keys(node), {'foo'})
39    self.assertTrue(anno.hasanno(node, 'foo'))
40    self.assertEqual(anno.getanno(node, 'foo'), 3)
41    self.assertEqual(anno.getanno(node, 'bar', default=7), 7)
42
43    anno.delanno(node, 'foo')
44
45    self.assertEqual(anno.keys(node), set())
46    self.assertFalse(anno.hasanno(node, 'foo'))
47    with self.assertRaises(AttributeError):
48      anno.getanno(node, 'foo')
49    self.assertIsNone(anno.getanno(node, 'foo', default=None))
50
51  def test_copy(self):
52    node_1 = ast.Name()
53    anno.setanno(node_1, 'foo', 3)
54
55    node_2 = ast.Name()
56    anno.copyanno(node_1, node_2, 'foo')
57    anno.copyanno(node_1, node_2, 'bar')
58
59    self.assertTrue(anno.hasanno(node_2, 'foo'))
60    self.assertFalse(anno.hasanno(node_2, 'bar'))
61
62  def test_duplicate(self):
63    node = ast.If(
64        test=ast.Num(1),
65        body=[ast.Expr(ast.Name('bar', ast.Load()))],
66        orelse=[])
67    anno.setanno(node, 'spam', 1)
68    anno.setanno(node, 'ham', 1)
69    anno.setanno(node.body[0], 'ham', 1)
70
71    anno.dup(node, {'spam': 'eggs'})
72
73    self.assertTrue(anno.hasanno(node, 'spam'))
74    self.assertTrue(anno.hasanno(node, 'ham'))
75    self.assertTrue(anno.hasanno(node, 'eggs'))
76    self.assertFalse(anno.hasanno(node.body[0], 'eggs'))
77
78
79if __name__ == '__main__':
80  test.main()
81