Coverage for gws-app/gws/lib/style/_test.py: 0%

61 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Tests for the style module.""" 

2 

3import gws 

4import gws.lib.style as style 

5import gws.test.util as u 

6 

7_test_against_dict = { 

8 'fill': None, 

9 'icon': None, 

10 'label_align': 'center', 

11 'label_font_family': 'sans-serif', 

12 'label_font_size': 12, 

13 'label_font_style': 'normal', 

14 'label_font_weight': 'normal', 

15 'label_line_height': 1, 

16 'label_max_scale': 1000000000, 

17 'label_min_scale': 0, 

18 'label_offset_x': None, 

19 'label_offset_y': None, 

20 'label_placement': 'middle', 

21 'label_stroke_dasharray': [], 

22 'label_stroke_dashoffset': 0, 

23 'label_stroke_linecap': 'butt', 

24 'label_stroke_linejoin': 'miter', 

25 'label_stroke_miterlimit': 2222, 

26 'label_stroke_width': 0, 

27 'marker_fill': 'rgb(255,90,33)', 

28 'marker_size': 0, 

29 'marker_stroke_dasharray': [], 

30 'marker_stroke_dashoffset': 0, 

31 'marker_stroke_linecap': 'butt', 

32 'marker_stroke_linejoin': 'miter', 

33 'marker_stroke_miterlimit': 0, 

34 'marker_stroke_width': 0, 

35 'offset_x': 0, 

36 'offset_y': 0, 

37 'parsed_icon': None, 

38 'point_size': 10, 

39 'stroke': None, 

40 'stroke_dasharray': [], 

41 'stroke_dashoffset': 0, 

42 'stroke_linecap': 'butt', 

43 'stroke_linejoin': 'miter', 

44 'stroke_miterlimit': 0, 

45 'stroke_width': 0, 

46 'with_geometry': 'all', 

47 'with_label': 'all', 

48} 

49 

50_values = {'fill': None, 

51 'stroke': None, 

52 'stroke_dasharray': [], 

53 'stroke_dashoffset': 0, 

54 'stroke_linecap': 'butt', 

55 'stroke_linejoin': 'miter', 

56 'stroke_miterlimit': 0, 

57 'stroke_width': 0, 

58 'marker_size': 0, 

59 'marker_stroke_dasharray': [], 

60 'marker_stroke_dashoffset': 0, 

61 'marker_stroke_linecap': 'butt', 

62 'marker_stroke_linejoin': 'miter', 

63 'marker_stroke_miterlimit': 0, 

64 'marker_stroke_width': 0, 

65 'with_geometry': 'all', 

66 'with_label': 'all', 

67 'label_align': 'center', 

68 'label_font_family': 'sans-serif', 

69 'label_font_size': 12, 

70 'label_font_style': 'normal', 

71 'label_font_weight': 'normal', 

72 'label_line_height': 1, 

73 'label_max_scale': 1000000000, 

74 'label_min_scale': 0, 

75 'label_offset_x': None, 

76 'label_offset_y': None, 

77 'label_placement': 'middle', 

78 'label_stroke_dasharray': [], 

79 'label_stroke_dashoffset': 0, 

80 'label_stroke_linecap': 'butt', 

81 'label_stroke_linejoin': 'miter', 

82 'label_stroke_miterlimit': 0, 

83 'label_stroke_width': 0, 

84 'point_size': 10, 

85 'icon': None, 'parsed_icon': None, 

86 'offset_x': 0, 'offset_y': 0, 

87 'marker_fill': 'rgb(255,90,33)', 

88 'label_fill': 'foo'} 

89 

90_test_against_obj = style.core.Object('', 

91 '__label-stroke_miterlimit: 2222; marker_fill: rgb(255, 90, 33)', 

92 _values) 

93 

94 

95# tests for core 

96def test_from_dict(): 

97 dic = {'text': '__label-stroke_miterlimit: 2222; marker_fill: rgb(255, 90, 33)', 

98 'values': {'with_geometry': 'ba', 'label_fill': 'foo'}} 

99 

100 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

101 ret = style.core.from_dict(dic, opt) 

102 ret_dic = ret.__dict__ 

103 assert ret_dic.__str__() == _test_against_obj.__dict__.__str__() 

104 

105 

106def test_from_config(): 

107 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

108 cfg = style.core.Config() 

109 cfg.set('cssSelector', '') 

110 cfg.set('text', '__label-stroke_miterlimit: 2222; marker_fill: rgb(255, 90, 33)') 

111 cfg.set('values', {'with_geometry': 'ba', 'label_fill': 'foo'}) 

112 ret = style.core.from_config(cfg, opt) 

113 ret_dic = ret.__dict__ 

114 assert ret_dic.__str__() == _test_against_obj.__dict__.__str__() 

115 

116 

117def test_from_props(): 

118 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

119 p = style.core.Props() 

120 p.set('cssSelector', '') 

121 _test_against_obj.text = '' 

122 _values['label_stroke_miterlimit'] = 0 

123 _values.pop('marker_fill') 

124 _test_against_obj.values = _values 

125 p.set('values', {'with_geometry': 'ba', 'label_fill': 'foo'}) 

126 ret = style.core.from_props(p, opt) 

127 ret_dic = ret.__dict__ 

128 assert ret_dic.__str__() == _test_against_obj.__dict__.__str__() 

129 

130 

131def test_to_data_url_empty(): 

132 icon = style.icon.ParsedIcon() 

133 assert style.icon.to_data_url(icon) == '' 

134 

135 

136# tests for icon 

137def test_icon(): 

138 url = 'https://mdn.dev/archives/media/attachments/2012/07/09/3075/89b1e0a26e8421e19f907e0522b188bd/svgdemo1.xml' 

139 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

140 icon = style.icon.parse(url, opt) 

141 url2 = style.icon.to_data_url(icon) 

142 icon2 = style.icon.parse(url2, opt) 

143 assert icon2.__dict__['svg'].__dict__ == icon.__dict__['svg'].__dict__ 

144 

145 

146def test_parse_untrusted(): 

147 url = 'https://mdn.dev/archives/media/attachments/2012/07/09/3075/89b1e0a26e8421e19f907e0522b188bd/svgdemo1.xml' 

148 opt = style.parser.Options(trusted=False, strict=True, imageDirs=()) 

149 with u.raises(Exception): 

150 style.icon.parse(url, opt) 

151 

152 

153# tests for parser 

154def test_parse_dict(): 

155 dic = {'__label-stroke_miterlimit': 2222, 

156 'marker_fill': 'rgb(255, 90, 33)', } 

157 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

158 assert style.parser.parse_dict(dic, opt) == _test_against_dict 

159 

160 

161def test_parse_dict_error(): 

162 dic = {'__label-stroke_miterlimit': 2222, 

163 'marker_fill': 'fo', 

164 'with_geometry': 'ba', 

165 'label_fill': 'fo'} 

166 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

167 with u.raises(Exception): 

168 assert style.parser.parse_dict(dic, opt) 

169 

170 

171def test_parse_text(): 

172 text = ('__label-stroke_miterlimit: 2222; ' 

173 ';' 

174 # ' : ;' 

175 'marker_fill: rgb(255, 90, 33)') 

176 opt = style.parser.Options(trusted=True, strict=True, imageDirs=()) 

177 assert style.parser.parse_text(text, opt) == _test_against_dict