• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 9507147410

13 Jun 2024 09:40PM UTC coverage: 50.521% (-18.7%) from 69.177%
9507147410

push

github

web-flow
Merge pull request #2062 from alerque/plug-fluent-leak

Link document.language setting more closely with Fluent locale

15 of 15 new or added lines in 3 files covered. (100.0%)

3244 existing lines in 65 files now uncovered.

8586 of 16995 relevant lines covered (50.52%)

4659.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/types/length.lua
1
--- SILE length type.
2
-- Lengths are composed of 3 `measurement`s: a length, a stretch, and a shrink. Each part internally is just
3
-- a measurement, but combined describe a flexible length that is allowed to grow up to the amout defined by stretch or
4
-- compress up to the amount defined by shrink.
5
-- @types length
6

7
local function _error_if_not_number (a)
8
   if type(a) ~= "number" then
204✔
9
      SU.error("We tried to do impossible arithmetic on a " .. SU.type(a) .. ". (That's a bug)", true)
×
10
   end
11
end
12

13
--- @type length
14
local length = pl.class()
5✔
15
length.type = "length"
5✔
16

17
length.length = nil
5✔
18
length.stretch = nil
5✔
19
length.shrink = nil
5✔
20

21
--- Constructor.
22
-- @tparam measurement spec A measurement or value that can be cast to a measurement.
23
-- @tparam[opt=0] measurement stretch A measurement describing how much the length is allowed to grow.
24
-- @tparam[opt=0] measurement shrink A measurement describing how much the length is allowed to grow.
25
-- @treturn length
26
-- @usage
27
-- SILE.types.length("6em", "4pt", "2pt")
28
-- SILE.types.length("6em plus 4pt minus 2pt")
29
-- SILE.types.length(30, 4, 2)
30
function length:_init (spec, stretch, shrink)
5✔
31
   if stretch or shrink then
12,370✔
32
      self.length = SILE.types.measurement(spec or 0)
5,246✔
33
      self.stretch = SILE.types.measurement(stretch or 0)
5,246✔
34
      self.shrink = SILE.types.measurement(shrink or 0)
5,246✔
35
   elseif type(spec) == "number" then
9,747✔
36
      self.length = SILE.types.measurement(spec)
7,532✔
37
   elseif SU.type(spec) == "measurement" then
11,962✔
38
      self.length = spec
157✔
39
   elseif SU.type(spec) == "glue" then
11,648✔
UNCOV
40
      self.length = SILE.types.measurement(spec.width.length or 0)
×
UNCOV
41
      self.stretch = SILE.types.measurement(spec.width.stretch or 0)
×
UNCOV
42
      self.shrink = SILE.types.measurement(spec.width.shrink or 0)
×
43
   elseif type(spec) == "table" then
5,824✔
44
      self.length = SILE.types.measurement(spec.length or 0)
4,786✔
45
      self.stretch = SILE.types.measurement(spec.stretch or 0)
4,786✔
46
      self.shrink = SILE.types.measurement(spec.shrink or 0)
4,786✔
47
   elseif type(spec) == "string" then
3,431✔
48
      local amount = tonumber(spec)
104✔
49
      if type(amount) == "number" then
104✔
UNCOV
50
         self:_init(amount)
×
51
      else
52
         local parsed = SILE.parserBits.length:match(spec)
104✔
53
         if not parsed then
104✔
54
            SU.error("Could not parse length '" .. spec .. "'")
×
55
         end
56
         self:_init(parsed)
104✔
57
      end
58
   end
59
   if not self.length then
12,370✔
60
      self.length = SILE.types.measurement()
6,654✔
61
   end
62
   if not self.stretch then
12,370✔
63
      self.stretch = SILE.types.measurement()
14,500✔
64
   end
65
   if not self.shrink then
12,370✔
66
      self.shrink = SILE.types.measurement()
14,500✔
67
   end
68
end
69

70
function length:absolute ()
5✔
71
   return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
4,180✔
72
end
73

74
function length:negate ()
5✔
75
   return self:__unm()
×
76
end
77

78
function length:tostring ()
5✔
79
   return self:__tostring()
×
80
end
81

82
function length:tonumber ()
5✔
83
   return self.length:tonumber()
7,975✔
84
end
85

86
function length:__tostring ()
5✔
UNCOV
87
   local str = tostring(self.length)
×
UNCOV
88
   if self.stretch.amount ~= 0 then
×
UNCOV
89
      str = str .. " plus " .. tostring(self.stretch)
×
90
   end
UNCOV
91
   if self.shrink.amount ~= 0 then
×
92
      str = str .. " minus " .. tostring(self.shrink)
×
93
   end
UNCOV
94
   return str
×
95
end
96

97
function length:__add (other)
5✔
98
   if type(self) == "number" then
1,324✔
99
      self, other = other, self
327✔
100
   end
101
   other = SU.cast("length", other)
2,648✔
102
   return SILE.types.length(self.length + other.length, self.stretch + other.stretch, self.shrink + other.shrink)
5,296✔
103
end
104

105
-- See usage comments on SILE.types.measurement:___add()
106
function length:___add (other)
5✔
107
   if SU.type(other) ~= "length" then
8,992✔
108
      self.length:___add(other)
454✔
109
   else
110
      self.length:___add(other.length)
4,269✔
111
      self.stretch:___add(other.stretch)
4,269✔
112
      self.shrink:___add(other.shrink)
4,269✔
113
   end
114
   return nil
4,496✔
115
end
116

117
function length:__sub (other)
5✔
118
   local result = SILE.types.length(self)
1,389✔
119
   other = SU.cast("length", other)
2,778✔
120
   result.length = result.length - other.length
2,778✔
121
   result.stretch = result.stretch - other.stretch
2,778✔
122
   result.shrink = result.shrink - other.shrink
2,778✔
123
   return result
1,389✔
124
end
125

126
-- See usage comments on SILE.types.measurement:___add()
127
function length:___sub (other)
5✔
128
   self.length:___sub(other.length)
256✔
129
   self.stretch:___sub(other.stretch)
256✔
130
   self.shrink:___sub(other.shrink)
256✔
131
   return nil
256✔
132
end
133

134
function length:__mul (other)
5✔
135
   if type(self) == "number" then
204✔
UNCOV
136
      self, other = other, self
×
137
   end
138
   _error_if_not_number(other)
204✔
139
   local result = SILE.types.length(self)
204✔
140
   result.length = result.length * other
408✔
141
   result.stretch = result.stretch * other
408✔
142
   result.shrink = result.shrink * other
408✔
143
   return result
204✔
144
end
145

146
function length:__div (other)
5✔
UNCOV
147
   local result = SILE.types.length(self)
×
UNCOV
148
   _error_if_not_number(other)
×
UNCOV
149
   result.length = result.length / other
×
UNCOV
150
   result.stretch = result.stretch / other
×
UNCOV
151
   result.shrink = result.shrink / other
×
UNCOV
152
   return result
×
153
end
154

155
function length:__unm ()
5✔
UNCOV
156
   local result = SILE.types.length(self)
×
UNCOV
157
   result.length = result.length:__unm()
×
UNCOV
158
   return result
×
159
end
160

161
function length:__lt (other)
5✔
162
   local a = SU.cast("number", self)
2,063✔
163
   local b = SU.cast("number", other)
2,063✔
164
   return a - b < 0
2,063✔
165
end
166

167
function length:__le (other)
5✔
UNCOV
168
   local a = SU.cast("number", self)
×
UNCOV
169
   local b = SU.cast("number", other)
×
UNCOV
170
   return a - b <= 0
×
171
end
172

173
function length:__eq (other)
5✔
UNCOV
174
   local a = SU.cast("length", self)
×
UNCOV
175
   local b = SU.cast("length", other)
×
UNCOV
176
   return a.length == b.length and a.stretch == b.stretch and a.shrink == b.shrink
×
177
end
178

179
return length
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc