Skip to content
Myr

test compares random filter to random call with same seed

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 92c31d6 · Browse files · 1 file+14 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−10 removed

  • tests:+14 added−10 removed

1 file+14 added−10 removed

modified:tests/test_filters.py+14 added-10 removed
181 unmodified lines
182 tmpl = env.from_string('''{{ data|pprint }}''')183 data = list(range(1000))184 assert tmpl.render(data=data) == pformat(data)185186 def test_random1(self, env):187 tmpl = env.from_string('''{{ seq|random }}''')188 seq = list(range(100))189 for _ in range(10):190 assert int(tmpl.render(seq=seq)) in seq186 def test_random(self, env, request):187 # restore the random state when the test ends188 state = random.getstate()189 request.addfinalizer(lambda: random.setstate(state))190 # generate the random values from a known seed191 random.seed('jinja', version=2)192 expected = [random.choice('1234567890') for _ in range(10)]193192 def test_random2(self, env, monkeypatch):193 monkeypatch.setattr(random, 'choice', lambda seq: seq[0])194 tmpl = env.from_string('''{{ range(100)|random }}''')195 monkeypatch.setattr(random, 'choice', lambda seq: seq[2])196 assert tmpl.render() == '2'194 # check that the random sequence is generated again by a template195 # ensures that filter result is not constant folded196 random.seed('jinja', version=2)197 t = env.from_string('{{ "1234567890"|random }}')198199 for value in expected:200 assert t.render() == value201202 def test_reverse(self, env):203 tmpl = env.from_string('{{ "foobar"|reverse|join }}|'204 '{{ [1, 2, 3]|reverse|list }}')205 assert tmpl.render() == 'raboof|[3, 2, 1]'
397 unmodified lines