Skip to content

Commit 6a85da0

Browse files
author
hhas
committed
* in TestTools, added numeric range check, fixed numeric equality check bug
* in Number, implemented (though not yet debugged) `number format definition` support in `parse number`, `format number`
1 parent 19edf37 commit 6a85da0

File tree

9 files changed

+99
-48
lines changed

9 files changed

+99
-48
lines changed

Number.scptd/Contents/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<key>name</key>
2424
<string>ScriptWindowState</string>
2525
<key>positionOfDivider</key>
26-
<real>522</real>
26+
<real>802</real>
2727
<key>savedFrame</key>
28-
<string>156 33 698 744 0 0 1280 777 </string>
28+
<string>1095 33 698 1024 0 0 1920 1057 </string>
2929
<key>selectedTab</key>
3030
<string>log</string>
3131
</dict>

Number.scptd/Contents/Resources/Number.sdef

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,109 @@
44

55

66

7-
<suite name="General Operations" code="****">
8-
9-
7+
<suite name="Number Conversion" code="****">
108

119
<command name="parse number" code="Mth:PNum" description="parse text as a number">
1210
<direct-parameter type="text"/>
13-
<parameter name="using" code="Usin" type="FNSt" optional="yes" description="(default: default format)"/>
11+
<parameter name="using" code="Usin" optional="yes" description="(default: default format)">
12+
<type type="MthZ"/>
13+
<type type="number format record"/>
14+
</parameter>
1415
<parameter name="for locale" code="Loca" optional="yes" description="a locale identifier, e.g. “en_US” (default: no locale)">
1516
<type type="text"/>
1617
<type type="LclE"/>
1718
</parameter>
1819
<result type="number"/>
1920
<documentation>
2021
<html><![CDATA[
21-
22-
<p>By default, the <code>parse number</code> command converts numeric text in canonical format to an integer/real number. Unlike coercing a text value to <code>number</code>, which parses the text according to the current user’s localization settings, this always uses the same numerical syntax as AppleScript itself. For example:</p>
22+
<h3>Basic Use</h3>
23+
24+
<p>By default, the <code>parse number</code> command converts numeric text in canonical format to an integer/real number. Unlike coercing a text value to <code>number</code>, which parses the text according to the current user’s localization settings, <code>format number</code> <em>always</em> uses the same numerical syntax as the AppleScript language, unless additional formatting and/or locale settings are explicitly given.</p>
25+
26+
<p>For example, on a US-localized system, coercing <code>"3.14"</code> to a number produces <code>3.14</code>:</p>
27+
28+
<pre><code>"3.14" as number → 3.14 -- localized conversion
29+
30+
parse number "3.14" → 3.14 -- canonical conversion</code></pre>
2331
24-
<pre><code>"3.14" as number → 3.14 (USA)
25-
"3,14" as number → 3.14 (Germany)
32+
<p>On a German system, however, the same text-to-number coercion requires the decimal separator to be a comma, not a period:</p>
2633
27-
convert text to number "3.14" → 3.14 (USA)
28-
convert text to number "3.14" → 3.14 (Germany)</code></pre>
34+
<pre><code>"3.14" as number → Error: Can’t make "3.14" into type number.
35+
36+
<strong>"3,14"</strong> as number → 3.14 -- localized conversion
37+
38+
parse number "3.14" → 3.14 -- canonical conversion</code></pre>
39+
40+
<p>Using the <code>parse number</code> command instead of coercing the number to text ensures a consistent result, no matter where the script is run.</p>
41+
42+
<!-- TO DO: custom formatting discussion and examples -->
2943
3044
]]></html>
3145
</documentation>
3246
</command>
3347

3448
<command name="format number" code="Mth:FNum" description="format a number as text">
3549
<direct-parameter type="number"/>
36-
<parameter name="using" code="Usin" type="FNSt" optional="yes" description="(default: default format)"/>
50+
<parameter name="using" code="Usin" optional="yes" description="(default: default format)">
51+
<type type="MthZ"/>
52+
<type type="text"/>
53+
<type type="number format record"/>
54+
</parameter>
3755
<parameter name="for locale" code="Loca" optional="yes" description="a locale identifier, e.g. “en_US” (default: no locale)">
3856
<type type="text"/>
3957
<type type="LclE"/>
4058
</parameter>
4159
<result type="text"/>
4260
<documentation>
4361
<html><![CDATA[
62+
<h3>Basic Use</h3>
4463
45-
<p>By default, the <code>format number</code> command converts an integer/real number to numeric text in canonical format. Unlike coercing an integer or real value to <code>text</code>, which formats the text according to the current user’s localization settings, <code>format number</code> always uses the same numerical syntax as AppleScript itself, unless a locale is explicitly specified.</p>
64+
<p>By default, the <code>format number</code> command converts an integer/real number to numeric text in canonical format. Unlike coercing an integer or real value to <code>text</code>, which formats the text according to the current user’s localization settings, <code>format number</code> <em>always</em> uses the same numerical syntax as the AppleScript language, unless additional formatting and/or locale settings are explicitly given.</p>
4665
4766
<p>For example, on a US-localized system, coercing <code>3.14</code> to text produces <code>"3.14"</code>:</p>
4867
49-
<pre><code>3.14 as text → "3.14"
68+
<pre><code>3.14 as text → "3.14" -- localized conversion
69+
70+
format number 3.14 → "3.14" -- canonical conversion</code></pre>
71+
72+
<p>On a German system, however, the same number-to-text coercion uses a comma instead of a period as the decimal separator:</p>
5073
51-
format number 3.14 → "3.14"</code></pre>
74+
<pre><code>3.14 as text → <strong>"3,14"</strong> -- localized conversion
5275
53-
whereas on a German system, the same coercion produces <code>"3,14"</code> instead, using a comma instead of a period as the decimal separator:
76+
format number 3.14 → "3.14" -- canonical conversion</code></pre>
5477
55-
<pre><code>3.14 as text → <strong>"3,14"</strong>
78+
<p>Using the <code>format number</code> command instead of coercing the number to text ensures a consistent result, no matter where the script is run.</p>
5679
57-
format number 3.14 → "3.14"</code></pre>
80+
<!-- TO DO: custom formatting discussion and examples -->
5881
59-
<p>By disregarding the current locale, the <code>format number</code> command ensures the same text value will be produced no matter where the script is run.</p>
6082
]]></html>
6183
</documentation>
6284
</command>
6385

64-
<enumeration name="FNSt" code="FNSt">
65-
<enumerator name="default format" code="FNSD"/>
66-
<enumerator name="integer format" code="FNS0"/>
67-
<enumerator name="decimal format" code="FNS1"/>
68-
<enumerator name="currency format" code="FNS2"/>
69-
<enumerator name="percent format" code="FNS3"/>
70-
<enumerator name="scientific format" code="FNS4"/>
71-
<enumerator name="word format" code="FNS5"/>
86+
87+
<record-type name="number format definition" code="MthR">
88+
<property name="basic format" code="MthA">
89+
<type type="MthZ"/>
90+
<type type="text"/>
91+
</property>
92+
<property name="minimum decimal places" code="MthB" type="integer"/>
93+
<property name="maximum decimal places" code="MthC" type="integer"/>
94+
<property name="minimum significant digits" code="MthD" type="integer"/>
95+
<property name="maximum significant digits" code="MthE" type="integer"/>
96+
<property name="decimal separator" code="MthF" type="text"/>
97+
<property name="grouping separator" code="MthG" type="text"/>
98+
<property name="rounding behavior" code="MthH" type="MRnd"/>
99+
</record-type>
100+
101+
102+
<enumeration name="MthZ" code="MthZ">
103+
<enumerator name="default format" code="Mth0"/>
104+
<enumerator name="integer format" code="Mth1"/>
105+
<enumerator name="decimal format" code="Mth2"/>
106+
<enumerator name="scientific format" code="Mth3"/>
107+
<enumerator name="percent format" code="Mth4"/>
108+
<enumerator name="currency format" code="Mth5"/>
109+
<enumerator name="word format" code="Mth6"/>
72110
</enumeration>
73111

74112
<enumeration name="LclE" code="LclE">
@@ -97,9 +135,12 @@ format number 3.14 → "3.14"</code></pre>
97135
<type type="integer" list="yes"/>
98136
</result>
99137
</command>
100-
101-
102-
138+
139+
</suite>
140+
141+
142+
143+
<suite name="General Operations" code="****">
103144

104145
<command name="deg2rad" code="Mth:DeRa" description="convert an angle from degrees to radians">
105146
<direct-parameter type="real"/>
@@ -161,14 +202,10 @@ cmp {(0.7 * 0.7), 0.49} → 0 (i.e. the numbers are "equal")</code></pre>
161202

162203

163204
<command name="round number" code="Mth:RouN" description="round a number to the specified number of places">
164-
<direct-parameter type="real"/>
205+
<direct-parameter type="number"/>
165206
<parameter name="to places" code="Plac" type="integer" optional="yes" description="The maximum number of decimal places that can appear in the result (default: 0). If 0, the result is an integer; if greater than 0, the number's least significant digits are also rounded."/>
166-
<parameter name="by" code="Dire" type="MRnd" optional="yes" description="the rounding direction (default: ‘rounding halves to even’)"/>
167-
<result>
168-
<type type="real"/>
169-
<type type="integer"/>
170-
</result>
171-
<!-- TO DO: need documentation to fully explain each rounding behavior -->
207+
<parameter name="by" code="Dire" type="MRnd" optional="yes" description="the rounding direction (default: rounding halves to even)"/>
208+
<result type="number"/>
172209
</command>
173210

174211

Binary file not shown.

TestTools.scptd/Contents/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<key>name</key>
2424
<string>ScriptWindowState</string>
2525
<key>positionOfDivider</key>
26-
<real>511</real>
26+
<real>791</real>
2727
<key>savedFrame</key>
28-
<string>189 33 923 744 0 0 1280 777 </string>
28+
<string>420 33 923 1024 0 0 1920 1057 </string>
2929
<key>selectedTab</key>
3030
<string>log</string>
3131
</dict>
Binary file not shown.

TestTools.scptd/Contents/Resources/TestTools.sdef

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ end call_uppercaseText
8888
</documentation>
8989
</command>
9090

91+
92+
<command name="numeric range check" code="܆ë:NmRg" description="used in ‘assert test result’ to check that the actual result is within the expected numeric range">
93+
<parameter name="matching types" code="ETyp" type="boolean" optional="yes" description="if true, test will automatically fail if expected and actual numbers aren’t the same type; if false, test will pass as long as the number is in range (default: true)"/>
94+
<result type="script"/>
95+
<documentation>
96+
<html><![CDATA[
97+
<p>The <code>assert test result</code> command’s <code>is</code> parameter should be a two-number list of form <code>{<var>MIN</var>, <var>MAX</var>}</code>. For example:</p>
98+
99+
<pre><code>assert test result for <var>aResult</code> is {0.49, 0.51} -- result must be 0.5±0.01</code></pre>
100+
]]></html>
101+
</documentation>
102+
</command>
103+
104+
91105
<command name="exact error check" code="܆ë:ExEr" description="used in ‘assert test error’ to check that the actual error that occurs exactly matches the expected error information">
92106
<parameter name="using" code="Usin" type="record" optional="yes" description="a record of zero or more objects for checking specific error attributes (default: expected error attributes are checked for exact equality with actual error)"/>
93107
<result type="script"/>
@@ -123,37 +137,37 @@ end call_uppercaseText
123137
<dt><code>timerName()</code></dt>
124138
<dd><p>the timer name, if given</p>
125139
<ul>
126-
<li>Result: <tt>text</tt></i>
140+
<li>Result: <tt>text</tt></li>
127141
</ul>
128142
</dd>
129143
130144
<dt><code>startTimer()</code></dt>
131145
<dd><p>[re]start the timer (this does nothing if the timer is currently running)</p>
132146
<ul>
133-
<li>Result: <tt>script</tt> – the TimerObject returns itself, allowing timer creation and start commands to be chained for convenience, e.g. <code>(timer object)'s startTimer()</code></i>
147+
<li>Result: <tt>script</tt> – the TimerObject returns itself, allowing timer creation and start commands to be chained for convenience, e.g. <code>(timer object)'s startTimer()</code></li>
134148
</ul>
135149
</dd>
136150
137151
<dt><code>stopTimer()</code></dt>
138152
<dd><p>stop the timer (this does nothing if the timer is already stopped)</p>
139153
<ul>
140-
<li>Result: <tt>real</tt> – the number of seconds elapsed since timer was last started</i>
154+
<li>Result: <tt>real</tt> – the number of seconds elapsed since timer was last started</li>
141155
</ul>
142156
</dd>
143157
144158
<dl>
145159
<dt><code>elapsedTime()</code></dt>
146160
<dd><p>returns the number of seconds since running timer was last started</p>
147161
<ul>
148-
<li>Result: <tt>real</tt></i>
162+
<li>Result: <tt>real</tt></li>
149163
</ul>
150164
</dd>
151165
152166
<dl>
153167
<dt><code>totalTime()</code></dt>
154168
<dd><p>returns the total number of seconds the timer has been running</p>
155169
<ul>
156-
<li>Result: <tt>real</tt></i>
170+
<li>Result: <tt>real</tt></li>
157171
</ul>
158172
</dd>
159173
</dl>

TypeSupport.scptd/Contents/Info.plist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
<key>bundleDividerCollapsed</key>
1616
<false/>
1717
<key>bundlePositionOfDivider</key>
18-
<real>580</real>
18+
<real>682</real>
1919
<key>dividerCollapsed</key>
2020
<false/>
2121
<key>eventLogLevel</key>
2222
<integer>2</integer>
2323
<key>name</key>
2424
<string>ScriptWindowState</string>
2525
<key>positionOfDivider</key>
26-
<real>547</real>
26+
<real>827</real>
2727
<key>savedFrame</key>
28-
<string>389 33 879 744 0 0 1280 777 </string>
28+
<string>875 33 931 1024 0 0 1920 1057 </string>
2929
<key>selectedTab</key>
3030
<string>result</string>
3131
</dict>
Binary file not shown.

unittests/number.unittest.scpt

2.38 KB
Binary file not shown.

0 commit comments

Comments
 (0)