1# Copyright 2015 Google Inc. 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"""Tests for yapf.format_decision_state."""
15
16import textwrap
17import unittest
18
19from yapf.yapflib import format_decision_state
20from yapf.yapflib import pytree_utils
21from yapf.yapflib import style
22from yapf.yapflib import unwrapped_line
23
24from yapftests import yapf_test_helper
25
26
27class FormatDecisionStateTest(yapf_test_helper.YAPFTest):
28
29  @classmethod
30  def setUpClass(cls):
31    style.SetGlobalStyle(style.CreateChromiumStyle())
32
33  def testSimpleFunctionDefWithNoSplitting(self):
34    code = textwrap.dedent(r"""
35      def f(a, b):
36        pass
37      """)
38    uwlines = yapf_test_helper.ParseAndUnwrap(code)
39    uwline = unwrapped_line.UnwrappedLine(0, _FilterLine(uwlines[0]))
40    uwline.CalculateFormattingInformation()
41
42    # Add: 'f'
43    state = format_decision_state.FormatDecisionState(uwline, 0)
44    state.MoveStateToNextToken()
45    self.assertEqual('f', state.next_token.value)
46    self.assertFalse(state.CanSplit(False))
47
48    # Add: '('
49    state.AddTokenToState(False, True)
50    self.assertEqual('(', state.next_token.value)
51    self.assertFalse(state.CanSplit(False))
52    self.assertFalse(state.MustSplit())
53
54    # Add: 'a'
55    state.AddTokenToState(False, True)
56    self.assertEqual('a', state.next_token.value)
57    self.assertTrue(state.CanSplit(False))
58    self.assertFalse(state.MustSplit())
59
60    # Add: ','
61    state.AddTokenToState(False, True)
62    self.assertEqual(',', state.next_token.value)
63    self.assertFalse(state.CanSplit(False))
64    self.assertFalse(state.MustSplit())
65
66    # Add: 'b'
67    state.AddTokenToState(False, True)
68    self.assertEqual('b', state.next_token.value)
69    self.assertTrue(state.CanSplit(False))
70    self.assertFalse(state.MustSplit())
71
72    # Add: ')'
73    state.AddTokenToState(False, True)
74    self.assertEqual(')', state.next_token.value)
75    self.assertTrue(state.CanSplit(False))
76    self.assertFalse(state.MustSplit())
77
78    # Add: ':'
79    state.AddTokenToState(False, True)
80    self.assertEqual(':', state.next_token.value)
81    self.assertFalse(state.CanSplit(False))
82    self.assertFalse(state.MustSplit())
83
84    clone = state.Clone()
85    self.assertEqual(repr(state), repr(clone))
86
87  def testSimpleFunctionDefWithSplitting(self):
88    code = textwrap.dedent(r"""
89      def f(a, b):
90        pass
91      """)
92    uwlines = yapf_test_helper.ParseAndUnwrap(code)
93    uwline = unwrapped_line.UnwrappedLine(0, _FilterLine(uwlines[0]))
94    uwline.CalculateFormattingInformation()
95
96    # Add: 'f'
97    state = format_decision_state.FormatDecisionState(uwline, 0)
98    state.MoveStateToNextToken()
99    self.assertEqual('f', state.next_token.value)
100    self.assertFalse(state.CanSplit(False))
101
102    # Add: '('
103    state.AddTokenToState(True, True)
104    self.assertEqual('(', state.next_token.value)
105    self.assertFalse(state.CanSplit(False))
106
107    # Add: 'a'
108    state.AddTokenToState(True, True)
109    self.assertEqual('a', state.next_token.value)
110    self.assertTrue(state.CanSplit(False))
111
112    # Add: ','
113    state.AddTokenToState(True, True)
114    self.assertEqual(',', state.next_token.value)
115    self.assertFalse(state.CanSplit(False))
116
117    # Add: 'b'
118    state.AddTokenToState(True, True)
119    self.assertEqual('b', state.next_token.value)
120    self.assertTrue(state.CanSplit(False))
121
122    # Add: ')'
123    state.AddTokenToState(True, True)
124    self.assertEqual(')', state.next_token.value)
125    self.assertTrue(state.CanSplit(False))
126
127    # Add: ':'
128    state.AddTokenToState(True, True)
129    self.assertEqual(':', state.next_token.value)
130    self.assertFalse(state.CanSplit(False))
131
132    clone = state.Clone()
133    self.assertEqual(repr(state), repr(clone))
134
135
136def _FilterLine(uwline):
137  """Filter out nonsemantic tokens from the UnwrappedLines."""
138  return [
139      ft for ft in uwline.tokens
140      if ft.name not in pytree_utils.NONSEMANTIC_TOKENS
141  ]
142
143
144if __name__ == '__main__':
145  unittest.main()
146