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

sile-typesetter / sile / 8288578143

14 Mar 2024 09:39PM UTC coverage: 64.155% (-10.6%) from 74.718%
8288578143

Pull #1904

github

alerque
chore(core): Fixup ec6ed657 which didn't shim old pack styles properly
Pull Request #1904: Merge develop into master (commit to next release being breaking)

1648 of 2421 new or added lines in 107 files covered. (68.07%)

1843 existing lines in 77 files now uncovered.

10515 of 16390 relevant lines covered (64.15%)

3306.56 hits per line

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

91.35
/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
1,431✔
NEW
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()
97✔
15
length.type = "length"
97✔
16

17
length.length = nil
97✔
18
length.stretch = nil
97✔
19
length.shrink = nil
97✔
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)
97✔
31
  if stretch or shrink then
125,335✔
32
    self.length = SILE.types.measurement(spec or 0)
52,874✔
33
    self.stretch = SILE.types.measurement(stretch or 0)
52,874✔
34
    self.shrink = SILE.types.measurement(shrink or 0)
52,874✔
35
  elseif type(spec) == "number" then
98,898✔
36
    self.length = SILE.types.measurement(spec)
77,390✔
37
  elseif SU.type(spec) == "measurement" then
120,406✔
38
    self.length = spec
1,562✔
39
  elseif SU.type(spec) == "glue" then
117,282✔
NEW
40
    self.length = SILE.types.measurement(spec.width.length or 0)
×
NEW
41
    self.stretch = SILE.types.measurement(spec.width.stretch or 0)
×
NEW
42
    self.shrink = SILE.types.measurement(spec.width.shrink or 0)
×
43
  elseif type(spec) == "table" then
58,641✔
44
    self.length = SILE.types.measurement(spec.length or 0)
38,680✔
45
    self.stretch = SILE.types.measurement(spec.stretch or 0)
38,680✔
46
    self.shrink = SILE.types.measurement(spec.shrink or 0)
38,680✔
47
  elseif type(spec) == "string" then
39,301✔
48
    local amount = tonumber(spec)
1,264✔
49
    if type(amount) == "number" then
1,264✔
50
      self:_init(amount)
6✔
51
    else
52
      local parsed = SILE.parserBits.length:match(spec)
1,261✔
53
      if not parsed then SU.error("Could not parse length '"..spec.."'") end
1,261✔
54
      self:_init(parsed)
1,261✔
55
    end
56
  end
57
  if not self.length then self.length = SILE.types.measurement() end
163,372✔
58
  if not self.stretch then self.stretch = SILE.types.measurement() end
203,629✔
59
  if not self.shrink then self.shrink = SILE.types.measurement() end
203,629✔
60
end
61

62
function length:absolute ()
97✔
63
  return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
45,436✔
64
end
65

66
function length:negate ()
97✔
NEW
67
  return self:__unm()
×
68
end
69

70
function length:tostring ()
97✔
NEW
71
  return self:__tostring()
×
72
end
73

74
function length:tonumber ()
97✔
75
  return self.length:tonumber()
78,152✔
76
end
77

78
function length:__tostring ()
97✔
79
  local str = tostring(self.length)
3✔
80
  if self.stretch.amount ~= 0 then str = str .. " plus " .. tostring(self.stretch) end
4✔
81
  if self.shrink.amount  ~= 0 then str = str .. " minus " .. tostring(self.shrink) end
3✔
82
  return str
3✔
83
end
84

85
function length:__add (other)
97✔
86
  if type(self) == "number" then self, other = other, self end
12,902✔
87
  other = SU.cast("length", other)
25,804✔
88
  return SILE.types.length(self.length + other.length,
12,902✔
89
  self.stretch + other.stretch,
12,902✔
90
  self.shrink + other.shrink)
25,804✔
91
end
92

93
-- See usage comments on SILE.types.measurement:___add()
94
function length:___add (other)
97✔
95
  if SU.type(other) ~= "length" then
68,102✔
96
    self.length:___add(other)
3,826✔
97
  else
98
    self.length:___add(other.length)
32,138✔
99
    self.stretch:___add(other.stretch)
32,138✔
100
    self.shrink:___add(other.shrink)
32,138✔
101
  end
102
  return nil
34,051✔
103
end
104

105
function length:__sub (other)
97✔
106
  local result = SILE.types.length(self)
10,994✔
107
  other = SU.cast("length", other)
21,988✔
108
  result.length = result.length - other.length
21,988✔
109
  result.stretch = result.stretch - other.stretch
21,988✔
110
  result.shrink = result.shrink - other.shrink
21,988✔
111
  return result
10,994✔
112
end
113

114
-- See usage comments on SILE.types.measurement:___add()
115
function length:___sub (other)
97✔
116
  self.length:___sub(other.length)
1,988✔
117
  self.stretch:___sub(other.stretch)
1,988✔
118
  self.shrink:___sub(other.shrink)
1,988✔
119
  return nil
1,988✔
120
end
121

122
function length:__mul (other)
97✔
123
  if type(self) == "number" then self, other = other, self end
1,262✔
124
  _error_if_not_number(other)
1,262✔
125
  local result = SILE.types.length(self)
1,262✔
126
  result.length = result.length * other
2,524✔
127
  result.stretch = result.stretch * other
2,524✔
128
  result.shrink = result.shrink * other
2,524✔
129
  return result
1,262✔
130
end
131

132
function length:__div (other)
97✔
133
  local result = SILE.types.length(self)
169✔
134
  _error_if_not_number(other)
169✔
135
  result.length = result.length / other
338✔
136
  result.stretch = result.stretch / other
338✔
137
  result.shrink = result.shrink / other
338✔
138
  return result
169✔
139
end
140

141
function length:__unm ()
97✔
142
  local result = SILE.types.length(self)
26✔
143
  result.length = result.length:__unm()
52✔
144
  return result
26✔
145
end
146

147
function length:__lt (other)
97✔
148
  local a = SU.cast("number", self)
23,859✔
149
  local b = SU.cast("number", other)
23,859✔
150
  return a - b < 0
23,859✔
151
end
152

153
function length:__le (other)
97✔
154
  local a = SU.cast("number", self)
7✔
155
  local b = SU.cast("number", other)
7✔
156
  return a - b <= 0
7✔
157
end
158

159
function length:__eq (other)
97✔
NEW
160
  local a = SU.cast("length", self)
×
NEW
161
  local b = SU.cast("length", other)
×
NEW
162
  return a.length == b.length and a.stretch == b.stretch and a.shrink == b.shrink
×
163
end
164

165
return length
97✔
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

© 2026 Coveralls, Inc