# Copyright 2017 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # =*= License: GPL-3+ =*= import os import unittest import yarnhelper class GetNextMatchTests(unittest.TestCase): def test_raises_error_if_no_next_match(self): h = yarnhelper.YarnHelper() h.set_environment({}) with self.assertRaises(yarnhelper.Error): h.get_next_match() def test_returns_first_match_if_there(self): h = yarnhelper.YarnHelper() h.set_environment({ 'MATCH_1': 'first', }) self.assertEqual(h.get_next_match(), 'first') def test_returns_second_match_if_there(self): h = yarnhelper.YarnHelper() h.set_environment({ 'MATCH_1': 'first', 'MATCH_2': 'second', }) self.assertEqual(h.get_next_match(), 'first') self.assertEqual(h.get_next_match(), 'second') def test_raises_error_if_no_more_matches(self): h = yarnhelper.YarnHelper() h.set_environment({ 'MATCH_1': 'first', }) self.assertEqual(h.get_next_match(), 'first') with self.assertRaises(yarnhelper.Error): h.get_next_match() class PersistentVariableTests(unittest.TestCase): def setUp(self): # We need this so that tearDown works pass def tearDown(self): if os.path.exists(yarnhelper.variables_filename): os.remove(yarnhelper.variables_filename) def test_sets_variable_persistently(self): h = yarnhelper.YarnHelper() h.set_variable('FOO', 'bar') h2 = yarnhelper.YarnHelper() self.assertEqual(h2.get_variable('FOO'), 'bar') def test_get_returns_default_if_variable_not_set(self): h = yarnhelper.YarnHelper() self.assertEqual(h.get_variable('FOO', 'bar'), 'bar') def test_appends_to_empty_list(self): h = yarnhelper.YarnHelper() h.append_to_list('foo', 1) self.assertEqual(h.get_variable('foo'), [1]) class HttpTests(unittest.TestCase): def test_constructs_aliased_request(self): h = yarnhelper.YarnHelper() server = 'new.example.com' url = 'http://www.example.com/path' r = h.construct_aliased_http_request(server, 'GET', url) self.assertEqual(r.url, 'http://new.example.com/path') self.assertEqual(r.headers['Host'], 'www.example.com') class AssertionTests(unittest.TestCase): def test_assertEqual_asserts_equals_correctly(self): h = yarnhelper.YarnHelper() self.assertEqual(h.assertEqual(0, 0), None) def test_assertEqual_raises_error_for_nonequal_values(self): h = yarnhelper.YarnHelper() with self.assertRaises(yarnhelper.Error): h.assertEqual(0, 1) def test_assertNotEqual_asserts_nonequal_correct(self): h = yarnhelper.YarnHelper() self.assertEqual(h.assertNotEqual(0, 1), None) def test_assertNotEqual_raises_error_for_equal_values(self): h = yarnhelper.YarnHelper() with self.assertRaises(yarnhelper.Error): h.assertNotEqual(0, 0) def test_assertGreaterThan_raises_error_for_equal_values(self): h = yarnhelper.YarnHelper() with self.assertRaises(yarnhelper.Error): h.assertGreaterThan(0, 0) def test_assertGreaterThan_raises_error_for_unordered_values(self): h = yarnhelper.YarnHelper() with self.assertRaises(yarnhelper.Error): h.assertGreaterThan(0, 1) def test_assertIn_asserts_correctly(self): h = yarnhelper.YarnHelper() self.assertEqual(h.assertIn('ana', 'banana'), None) def test_assertIn_raises_error_for_false(self): h = yarnhelper.YarnHelper() with self.assertRaises(yarnhelper.Error): h.assertIn('nope', 'banana')