Coverage for tests/test_string.py: 100%

82 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-29 10:26 +0100

1""" 

2test for String() 

3""" 

4import unittest 

5 

6from stricto import String, Error 

7 

8 

9class TestString(unittest.TestCase): # pylint: disable=too-many-public-methods 

10 """ 

11 Test on Strings 

12 """ 

13 def test_error_type(self): 

14 """ 

15 Test error 

16 """ 

17 a = String() 

18 with self.assertRaises(Error) as e: 

19 a.set(12) 

20 self.assertEqual(e.exception.message, "Must be a string") 

21 a.set("yeah") 

22 self.assertEqual(a, "yeah") 

23 

24 def test_add(self): 

25 """ 

26 Add 2 strings 

27 """ 

28 a = String() 

29 a.set("foo") 

30 b = String() 

31 b.set("bar") 

32 c = a + b 

33 self.assertEqual(type(c), String) 

34 self.assertEqual(c, "foobar") 

35 

36 def test_compare(self): 

37 """ 

38 String comparison 

39 """ 

40 a = String() 

41 a.set("foo") 

42 b = String() 

43 b.set("bar") 

44 self.assertNotEqual(a, b) 

45 self.assertEqual(a, "foo") 

46 self.assertEqual(a == "foo", True) 

47 self.assertEqual(a != "foo", False) 

48 for c in [ b, "bar"]: 

49 self.assertEqual(a < c, False) 

50 self.assertEqual(a <= c, False) 

51 self.assertEqual(a > c, True) 

52 self.assertEqual(a >= c, True) 

53 

54 # check for non reference 

55 b.set(a) 

56 self.assertEqual(a, b) 

57 a.set("hop") 

58 self.assertNotEqual(a, b) 

59 

60 def test_len(self): 

61 """ 

62 length 

63 """ 

64 a = String() 

65 a.set("foo") 

66 self.assertEqual(len (a), 3) 

67 

68 def test_union(self): 

69 """ 

70 union 

71 """ 

72 a = String( union=['M', 'F' ]) 

73 with self.assertRaises(Error) as e: 

74 a.set("foo") 

75 self.assertEqual(e.exception.message, "not in list") 

76 a.set("F") 

77 self.assertEqual(a, 'F') 

78 

79 def test_union_error(self): 

80 """ 

81 union error 

82 """ 

83 a = String( union= 22 ) 

84 with self.assertRaises(Error) as e: 

85 a.set("M") 

86 self.assertEqual(e.exception.message, "Union constraint not list") 

87 

88 def test_not_null(self): 

89 """ 

90 String not null 

91 """ 

92 with self.assertRaises(Error) as e: 

93 a = String(notNone=True) 

94 self.assertEqual(e.exception.message, "Cannot be empty") 

95 a = String(notNone=True, default="") 

96 with self.assertRaises(Error) as e: 

97 a.set(None) 

98 self.assertEqual(e.exception.message, "Cannot be empty") 

99 

100 def test_default(self): 

101 """ 

102 test default value 

103 """ 

104 a = String(notNone=True, default="yoyo") 

105 self.assertEqual(a, "yoyo") 

106 

107 def test_count(self): 

108 """ 

109 test count function 

110 """ 

111 a = String(notNone=True, default="yoyo") 

112 self.assertEqual(a.count("y"), 2) 

113 

114 def test_regexp(self): 

115 """ 

116 test regexps 

117 """ 

118 # unique regexp 

119 a = String(regexp="^A") 

120 with self.assertRaises(Error) as e: 

121 a.set("Foo") 

122 self.assertEqual(e.exception.message, "Dont match regexp") 

123 a.set("AZERTY") 

124 

125 # list of regexp 

126 a = String(regexp=["^A", r".*Z$"]) 

127 with self.assertRaises(Error) as e: 

128 a.set("Foo") 

129 self.assertEqual(e.exception.message, "Dont match regexp") 

130 a.set("AtoZ") 

131 

132 # function return a regexp 

133 a = String(regexp=lambda value, root: r".*Z$") 

134 with self.assertRaises(Error) as e: 

135 a.set("Foo") 

136 self.assertEqual(e.exception.message, "Dont match regexp") 

137 a.set("AtoZ")