• 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
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import ast
22
23from tensorflow.python.autograph.pyct import anno
24from tensorflow.python.platform import test
25
26
27# TODO(mdan): Consider strong types instead of primitives.
28
29
30class AnnoTest(test.TestCase):
31
32  def test_basic(self):
33    node = ast.Name()
34
35    self.assertEqual(anno.keys(node), set())
36    self.assertFalse(anno.hasanno(node, 'foo'))
37    with self.assertRaises(AttributeError):
38      anno.getanno(node, 'foo')
39
40    anno.setanno(node, 'foo', 3)
41
42    self.assertEqual(anno.keys(node), {'foo'})
43    self.assertTrue(anno.hasanno(node, 'foo'))
44    self.assertEqual(anno.getanno(node, 'foo'), 3)
45    self.assertEqual(anno.getanno(node, 'bar', default=7), 7)
46
47    anno.delanno(node, 'foo')
48
49    self.assertEqual(anno.keys(node), set())
50    self.assertFalse(anno.hasanno(node, 'foo'))
51    with self.assertRaises(AttributeError):
52      anno.getanno(node, 'foo')
53    self.assertIsNone(anno.getanno(node, 'foo', default=None))
54
55  def test_copy(self):
56    node_1 = ast.Name()
57    anno.setanno(node_1, 'foo', 3)
58
59    node_2 = ast.Name()
60    anno.copyanno(node_1, node_2, 'foo')
61    anno.copyanno(node_1, node_2, 'bar')
62
63    self.assertTrue(anno.hasanno(node_2, 'foo'))
64    self.assertFalse(anno.hasanno(node_2, 'bar'))
65
66  def test_duplicate(self):
67    node = ast.If(
68        test=ast.Num(1),
69        body=[ast.Expr(ast.Name('bar', ast.Load()))],
70        orelse=[])
71    anno.setanno(node, 'spam', 1)
72    anno.setanno(node, 'ham', 1)
73    anno.setanno(node.body[0], 'ham', 1)
74
75    anno.dup(node, {'spam': 'eggs'})
76
77    self.assertTrue(anno.hasanno(node, 'spam'))
78    self.assertTrue(anno.hasanno(node, 'ham'))
79    self.assertTrue(anno.hasanno(node, 'eggs'))
80    self.assertFalse(anno.hasanno(node.body[0], 'eggs'))
81
82
83if __name__ == '__main__':
84  test.main()
85