Skip to content

Commit

Permalink
Merge pull request #363 from seandenigris/enh_number-thousands-sep
Browse files Browse the repository at this point in the history
[Enh]: Number Reading - Thousands Separators
  • Loading branch information
seandenigris authored Sep 10, 2024
2 parents 2cad103 + 65d800f commit fff9868
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
22 changes: 22 additions & 0 deletions source/Magritte-Model/MANumberDescription.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ MANumberDescription class >> defaultKind [
^ Number
]

{ #category : #'accessing-defaults' }
MANumberDescription class >> defaultThousandsSeparator [

^ $,
]

{ #category : #testing }
MANumberDescription class >> isAbstract [
^ false
Expand Down Expand Up @@ -41,3 +47,19 @@ MANumberDescription >> beNegative [
MANumberDescription >> bePositive [
self addCondition: (MACondition selector: #positive) labelled: 'No positive number was entered'
]

{ #category : #accessing }
MANumberDescription >> thousandsSeparator [

^ self
propertyAt: #thousandsSeparator
ifAbsent: [ self class defaultThousandsSeparator ]
]

{ #category : #accessing }
MANumberDescription >> thousandsSeparator: aCharacter [

^ self
propertyAt: #thousandsSeparator
putRemovingNil: aCharacter
]
7 changes: 5 additions & 2 deletions source/Magritte-Model/MAStringReader.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ MAStringReader >> visitMultipleOptionDescription: aDescription [

{ #category : #'visiting-description' }
MAStringReader >> visitNumberDescription: aDescription [
| isContentsValid |
isContentsValid := NumberParser isValidNumber: self contents.
| isContentsValid cleanedContents |
cleanedContents := self contents reject: [ :e | e = aDescription thousandsSeparator ].
isContentsValid := NumberParser isValidNumber: cleanedContents.
isContentsValid ifFalse: [ MAReadError signal ].

self stream: cleanedContents readStream.
super visitNumberDescription: aDescription
]

Expand Down
21 changes: 20 additions & 1 deletion source/Magritte-Tests-Model/MANumberDescriptionTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #MANumberDescriptionTest,
#superclass : #MAMagnitudeDescriptionTest,
#category : 'Magritte-Tests-Model-Description'
#category : #'Magritte-Tests-Model-Description'
}

{ #category : #testing }
Expand Down Expand Up @@ -85,6 +85,25 @@ MANumberDescriptionTest >> testFromString [
= -20 description: 'Negative numbers should be accepted'
]

{ #category : #private }
MANumberDescriptionTest >> testThousandsSeparatorCustom [

self
assert: (self description
thousandsSeparator: $.;
fromString: '20.000')
equals: 20000 "By default, commas should be ignored"
]

{ #category : #private }
MANumberDescriptionTest >> testThousandsSeparatorDefault [

"self assert: (self description fromString: '') isNil description: 'Empty string should be parsed to nil'."
self
assert: (self description fromString: '20,000')
equals: 20000 "By default, commas should be ignored"
]

{ #category : #tests }
MANumberDescriptionTest >> testValidateConditions [
]

0 comments on commit fff9868

Please sign in to comment.