How to use make_string_literal method in Pytest-bdd

Best Python code snippet using pytest-bdd

Run Pytest-bdd automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TokenizerTests.py

Source: TokenizerTests.py Github

copy
1#!/usr/bin/env python3
2
3import unittest
4
5from BeanPorter.bpcml.Tokenizer import Tokenizer
6from BeanPorter.bpcml.Token import Token
7
8
9class TokenizerTests(unittest.TestCase):
10
11  def test_can_recognize_contiguous_alphabets(self):
12    """
13    Test that it can recognize abc
14    """
15    tokenizer = Tokenizer()
16    expected_results = [
17      Token.make_string_literal("abc"),
18      Token.make_eof(),
19    ]
20    results = tokenizer.make_tokens("abc")
21    self.assertEqual(results, expected_results)
22
23  def test_can_recognize_dash_separated_alphabets(self):
24    """
25    Test that it can recognize abc
26    """
27    tokenizer = Tokenizer()
28    expected_results = [
29      Token.make_string_literal("abc-efg"),
30      Token.make_eof(),
31    ]
32    results = tokenizer.make_tokens("abc-efg")
33    self.assertEqual(results, expected_results)
34
35  def test_can_recognize_dot_separated_alphabets(self):
36    """
37    Test that it can recognize abc
38    """
39    tokenizer = Tokenizer()
40    expected_results = [
41      Token.make_string_literal("abc.efg"),
42      Token.make_eof(),
43    ]
44    results = tokenizer.make_tokens("abc.efg")
45    self.assertEqual(results, expected_results)
46
47  def test_can_recognize_dot_separated_triple_numbers(self):
48    """
49    Test that it can recognize abc
50    """
51    tokenizer = Tokenizer()
52    expected_results = [
53      Token.make_string_literal("123.456.789"),
54      Token.make_eof(),
55    ]
56    results = tokenizer.make_tokens("123.456.789")
57    self.assertEqual(results, expected_results)
58
59  def test_can_recognize_spaced_alphabets(self):
60    """
61    Test that it can recognize abc efb
62    """
63    tokenizer = Tokenizer()
64    expected_results = [
65      Token.make_string_literal("abc"), 
66      Token.make_space(" "), 
67      Token.make_string_literal("efg"),
68      Token.make_eof(),
69    ]
70    results = tokenizer.make_tokens("abc efg")
71    self.assertEqual(results, expected_results)
72
73  def test_can_recognize_label_of_colon_separated_alphabets(self):
74    """
75    Test that it can recognize A:B:C
76    """
77    tokenizer = Tokenizer()
78    expected_results = [
79      Token.make_string_literal("A:B:C"),
80      Token.make_eof(),
81    ]
82    results = tokenizer.make_tokens("A:B:C")
83    self.assertEqual(results, expected_results)
84
85  def test_can_recognize_integer_numbers(self):
86    """
87    Test that it can recognize 123
88    """
89    tokenizer = Tokenizer()
90    expected_results = [
91      Token.make_numeric_literal('123'),
92      Token.make_eof(),
93    ]
94    results = tokenizer.make_tokens('123')
95    self.assertEqual(results, expected_results)
96
97  def test_can_recognize_float_numbers(self):
98    """
99    Test that it can recognize 123.456
100    """
101    tokenizer = Tokenizer()
102    expected_results = [
103      Token.make_numeric_literal('123.456'),
104      Token.make_eof(),
105    ]
106    results = tokenizer.make_tokens("123.456")
107    self.assertEqual(results, expected_results)
108
109  def test_can_recognize_float_numbers_with_dot_end(self):
110    """
111    Test that it can recognize 123.0
112    """
113    tokenizer = Tokenizer()
114    expected_results = [
115      Token.make_numeric_literal('123.0'),
116      Token.make_eof(),
117    ]
118    results = tokenizer.make_tokens("123.0")
119    self.assertEqual(results, expected_results)
120
121  def test_can_recognize_float_numbers_with_dot_begin(self):
122    """
123    Test that it can recognize .456
124    """
125    tokenizer = Tokenizer()
126    expected_results = [
127      Token.make_numeric_literal('.456'),
128      Token.make_eof(),
129    ]
130    results = tokenizer.make_tokens(".456")
131    self.assertEqual(results, expected_results)
132
133  def test_can_recognize_plus_operator(self):
134    """
135    Test that it can recognize +
136    """
137    tokenizer = Tokenizer()
138    expected_results = [
139      Token.make_plus(),
140      Token.make_eof(),
141    ]
142    results = tokenizer.make_tokens("+")
143    self.assertEqual(results, expected_results)
144
145  def test_can_recognize_minus_operator(self):
146    """
147    Test that it can recognize -
148    """
149    tokenizer = Tokenizer()
150    expected_results = [
151      Token.make_minus(),
152      Token.make_eof(),
153    ]
154    results = tokenizer.make_tokens("-")
155    self.assertEqual(results, expected_results)
156
157  def test_can_recognize_minus_variable(self):
158    """
159    Test that it can recognize -$variable
160    """
161    tokenizer = Tokenizer()
162    expected_results = [
163      Token.make_minus(),
164      Token.make_dollar_sign(),
165      Token.make_string_literal("variable"), 
166      Token.make_eof(),
167    ]
168    results = tokenizer.make_tokens("-$variable")
169    self.assertEqual(results, expected_results)
170
171  def test_can_recognize_functioned_variable(self):
172    """
173    Test that it can recognize @func($variable)
174    """
175    tokenizer = Tokenizer()
176    expected_results = [
177      Token.make_at_sign(),
178      Token.make_string_literal("func"),
179      Token.make_left_paren(),
180      Token.make_dollar_sign(),
181      Token.make_string_literal("variable"),
182      Token.make_right_paren(),
183      Token.make_eof(),
184    ]
185    results = tokenizer.make_tokens("@func($variable)")
186    self.assertEqual(results, expected_results)
187
188  def test_can_recognize_comma(self):
189    """
190    Test that it can recognize ,
191    """
192    tokenizer = Tokenizer()
193    expected_results = [
194      Token.make_comma(), 
195      Token.make_eof(),
196    ]
197    results = tokenizer.make_tokens(",")
198    self.assertEqual(results, expected_results)
199
200  def test_can_recognize_comma_seperated_string_literal(self):
201    """
202    Test that it can recognize abc , efg
203    """
204    tokenizer = Tokenizer()
205    expected_results = [
206      Token.make_string_literal('abc'), 
207      Token.make_space(" "), 
208      Token.make_comma(), 
209      Token.make_space(" "), 
210      Token.make_string_literal('efg'), 
211      Token.make_eof(),
212    ]
213    results = tokenizer.make_tokens("abc , efg")
214    self.assertEqual(results, expected_results)
215
216if __name__ == '__main__':
217    unittest.main()
218
Full Screen

string.py

Source: string.py Github

copy
1def make_string_literal(string):
2    """
3    When inserting a string literal into an output source file, eg. "Hello World\n",
4    we do not want to print "Hello World\n" (which is "Hello World" followed by a newline),
5    but rather, "Hello World\\n", (which is "Hello World" followed by the sequence "\n").
6    """
7
8    string = string.replace("\\", "\\\\")
9    string = string.replace("\"", "\\\"")
10    string = string.replace("\n", "\\n")
11    string = string.replace("\t", "\\t")
12    string = string.replace("\r", "\\r")
13
14    return string
Full Screen

ExprTests.py

Source: ExprTests.py Github

copy
1#!/usr/bin/env python3
2
3import unittest
4
5from BeanPorter.bpcml.Exprs import ArithmeticExpr, NumExpr, StrLitExpr, VarRefExpr
6from BeanPorter.bpcml.Exprs import FuncArgListExpr, FuncCallExpr
7from BeanPorter.bpcml.Token import Token
8
9
10class ExprTests(unittest.TestCase):
11  
12  def test_num_expr(self):
13    expr = NumExpr.make(Token.make_numeric_literal('100'))
14    self.assertEqual(expr.__repr__(), 'NumExpr( 100 )')
15  
16  def test_str_expr(self):
17    expr = StrLitExpr.make(Token.make_string_literal('ABC:EFG:HIJ'))
18    self.assertEqual(expr.__repr__(), 'StrLitExpr( "ABC:EFG:HIJ" )')
19  
20  def test_var_ref_expr_open(self):
21    expr = VarRefExpr.make_open(Token.make_dollar_sign(), Token.make_string_literal('abc'))
22    self.assertEqual(expr.__repr__(), 'VarRefExpr( $abc )')
23  
24  def test_var_ref_expr_closed(self):
25    expr = VarRefExpr.make_closed(Token.make_dollar_sign(), Token.make_string_literal('abc'), Token.make_dollar_sign())
26    self.assertEqual(expr.__repr__(), 'VarRefExpr( $abc$ )')
27  
28  def test_arithmetic_expr(self):
29    num_expr = NumExpr.make(Token.make_numeric_literal('100'))
30    arith_expr = ArithmeticExpr.make_prefix(Token.make_plus(), num_expr)
31    self.assertEqual(arith_expr.__repr__(), """\
32ArithmeticExpr(
33  +
34  NumExpr( 100 ))""")
35
36  def test_func_arg_list_expr_empty(self):
37    func_arg_list_expr = FuncArgListExpr.make_empty()
38    self.assertEqual(func_arg_list_expr.__repr__(), 'FuncArgListExpr()')
39
40  def test_func_arg_list_expr_unary(self):
41    num_expr = NumExpr.make(Token.make_numeric_literal('100'))
42    func_arg_list_expr = FuncArgListExpr.make_unary(num_expr)
43    self.assertEqual(func_arg_list_expr.__repr__(), """\
44FuncArgListExpr(
45  NumExpr( 100 ))""")
46
47  def test_func_arg_list_expr_binary(self):
48    num_expr = NumExpr.make(Token.make_numeric_literal('100'))
49    func_arg_list_expr1 = FuncArgListExpr.make_unary(num_expr)
50    func_arg_list_expr2 = FuncArgListExpr.make_binary(
51      num_expr, 
52      Token.make_space(" "), 
53      Token.make_comma(), 
54      Token.make_space(" "), 
55      func_arg_list_expr1)
56    self.assertEqual(func_arg_list_expr2.__repr__(), """\
57FuncArgListExpr(
58  NumExpr( 100 ),
59  NumExpr( 100 ))""")
60
61  def test_func_call_empty_args(self):
62    func_arg_list_expr = FuncArgListExpr.make_empty()
63    func_call_expr = FuncCallExpr.make(Token.make_dollar_sign(), Token.make_string_literal("date"), Token.make_left_paren(), func_arg_list_expr, Token.make_right_paren())
64    self.assertEqual(func_call_expr.__repr__(), """\
65FuncCallExpr(
66  @ date
67  FuncArgListExpr())""")
68
69  def test_func_call_unary_arg(self):
70    num_expr = NumExpr.make(Token.make_numeric_literal('100'))
71    func_arg_list_expr = FuncArgListExpr.make_unary(num_expr)
72    func_call_expr = FuncCallExpr.make(Token.make_dollar_sign(), Token.make_string_literal("date"), Token.make_left_paren(), func_arg_list_expr, Token.make_right_paren())
73    self.assertEqual(func_call_expr.__repr__(), """\
74FuncCallExpr(
75  @ date
76  FuncArgListExpr(
77    NumExpr( 100 )))""")
78
79  def test_func_call_binary_arg(self):
80    num_expr = NumExpr.make(Token.make_numeric_literal('100'))
81    func_arg_list_expr1 = FuncArgListExpr.make_unary(num_expr)
82    func_arg_list_expr2 = FuncArgListExpr.make_binary(
83      num_expr, 
84      Token.make_space(" "), 
85      Token.make_comma(), 
86      Token.make_space(" "), 
87      func_arg_list_expr1)
88    func_call_expr = FuncCallExpr.make(Token.make_dollar_sign(), Token.make_string_literal("date"), Token.make_left_paren(), func_arg_list_expr2, Token.make_right_paren())
89    self.assertEqual(func_call_expr.__repr__(), """\
90FuncCallExpr(
91  @ date
92  FuncArgListExpr(
93    NumExpr( 100 ),
94    NumExpr( 100 )))""")
95
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Pytest-bdd on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)