Skip to content

Commit

Permalink
Add improvements based on code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gcotelli committed Jun 24, 2024
1 parent a9210c3 commit 33bd6ea
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 163 deletions.
4 changes: 1 addition & 3 deletions source/BaselineOfBuoy/BaselineOfBuoy.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ BaselineOfBuoy >> baselineLocalization: spec [
spec requires:
#( 'Buoy-Assertions' 'Buoy-Dynamic-Binding' 'Buoy-Metaprogramming-Extensions' ) ];
group: 'Deployment' with: 'Buoy-Localization';
package: 'Buoy-Localization-Extensions' with: [ spec requires: 'Buoy-Localization' ];
group: 'Deployment' with: 'Buoy-Localization-Extensions';
package: 'Buoy-Localization-Pharo-Extensions' with: [ spec requires: 'Buoy-Localization' ];
group: 'Deployment' with: 'Buoy-Localization-Pharo-Extensions';
package: 'Buoy-Localization-Tests'
with: [ spec requires: #( 'Buoy-Localization-Extensions' 'Dependent-SUnit-Extensions' ) ];
with: [ spec requires: #( 'Buoy-Localization-Pharo-Extensions' 'Dependent-SUnit-Extensions' ) ];
group: 'Tests' with: 'Buoy-Localization-Tests'
]

Expand Down
10 changes: 10 additions & 0 deletions source/Buoy-Deprecated-V8/LanguageRange.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Extension { #name : 'LanguageRange' }

{ #category : '*Buoy-Deprecated-V8' }
LanguageRange class >> from: aSubtagCollection [

self
deprecated: 'Use composedOf: instead'
transformWith: '`@receiver from: `@subtags' -> '`@receiver composedOf: `@subtags'.
^ self composedOf: aSubtagCollection
]
11 changes: 11 additions & 0 deletions source/Buoy-Deprecated-V8/LanguageTag.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Extension { #name : 'LanguageTag' }

{ #category : '*Buoy-Deprecated-V8' }
LanguageTag class >> from: aSubtagCollection [

self
deprecated: 'Use composedOf: instead'
transformWith: '`@receiver from: `@subtags' -> '`@receiver composedOf: `@subtags'.

^ self composedOf: aSubtagCollection
]
1 change: 1 addition & 0 deletions source/Buoy-Deprecated-V8/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : 'Buoy-Deprecated-V8' }
55 changes: 0 additions & 55 deletions source/Buoy-Localization-Extensions/String.extension.st

This file was deleted.

1 change: 0 additions & 1 deletion source/Buoy-Localization-Extensions/package.st

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ CharacterCollection >> asLanguageTag [
{ #category : '*Buoy-Localization-GS64-Extensions' }
CharacterCollection >> escaped [

^ self species new: self size streamContents: [ :result |
| stream |
stream := self readStream.
[ stream atEnd ] whileFalse: [ StringEscapingRule escape: stream next on: result ]
]
^ EscapingAlgorithm new escape: self
]

{ #category : '*Buoy-Localization-GS64-Extensions' }
Expand All @@ -37,19 +33,5 @@ CharacterCollection >> localizedWithAll: collection [
{ #category : '*Buoy-Localization-GS64-Extensions' }
CharacterCollection >> unescaped [

^ self species new: self size streamContents: [ :result |
| stream |
stream := self readStream.
[ stream atEnd ] whileFalse: [
| currentChar |
currentChar := stream next.
currentChar == $\
ifTrue: [
stream atEnd
ifTrue: [ AssertionFailed signal: 'Missing escape sequence' ]
ifFalse: [ StringEscapingRule unescape: stream next from: stream on: result ]
]
ifFalse: [ result nextPut: currentChar ]
]
]
^ EscapingAlgorithm new unescape: self
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
"
A NaturalLanguageTranslator is a dummy translator.
A NaturalLanguageTranslator is a placeholder for a language translator.
The localization framework is found in the gettext package usually
overriding this class completely.
As an alternative you can register a translator using
NaturalLanguageTranslator current: myTranslator
If this is done the messages will be dispatched to it
"
Class {
#name : 'NaturalLanguageTranslator',
Expand Down
37 changes: 37 additions & 0 deletions source/Buoy-Localization-Pharo-Extensions/String.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Extension { #name : 'String' }

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> asLanguageRange [

^ LanguageRange fromString: self
]

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> asLanguageTag [

^ LanguageTag fromString: self
]

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> escaped [

^ EscapingAlgorithm new escape: self
]

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> localized [

^ self localizedWithAll: #( )
]

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> localizedWithAll: collection [

^ NaturalLanguageTranslator current localize: self withAll: collection to: CurrentLocale value
]

{ #category : '*Buoy-Localization-Pharo-Extensions' }
String >> unescaped [

^ EscapingAlgorithm new unescape: self
]
107 changes: 78 additions & 29 deletions source/Buoy-Localization-Tests/LanguageRangeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -36,51 +36,100 @@ LanguageRangeTest >> testAsLanguageRange [
self
assert: '*' asLanguageRange equals: LanguageRange any;
assert: LanguageRange any asLanguageRange equals: LanguageRange any;
assert: 'es-AR' asLanguageRange equals: ( LanguageRange from: #( 'es' 'AR' ) ) asLanguageRange
assert: 'es-AR' asLanguageRange
equals: ( LanguageRange composedOf: #( 'es' 'AR' ) ) asLanguageRange
]

{ #category : 'tests' }
LanguageRangeTest >> testComparison [
LanguageRangeTest >> testCantCreateWhenLanguageCodeIsInvalid [

| range |
self
should: [ LanguageRange fromString: 'e' ]
raise: InstanceCreationFailed
withMessageText: 'ISO 639 language codes must be 2 or 3 letters.';
should: [ LanguageRange fromString: 'e2' ]
raise: InstanceCreationFailed
withMessageText: 'ISO 639 language codes must consist only of letters.'
]

range := LanguageRange from: #('en' 'US').
self
assert: range equals: ( LanguageRange fromString: 'en-us' );
assert: range hash equals: ( LanguageRange fromString: 'en-us' ) hash;
deny: range equals: LanguageRange any;
deny: range equals: ( LanguageRange from: #('en') )
{ #category : 'tests' }
LanguageRangeTest >> testCantCreateWhenRegionIsInvalid [

self
should: [ LanguageRange fromString: 'en-A3' ]
raise: InstanceCreationFailed
withMessageText: 'Supported ISO 3166-1 codes must be 2 letters.'
]

{ #category : 'tests' }
LanguageRangeTest >> testCreation [
LanguageRangeTest >> testCantCreateWhenScriptIsInvalid [

| range |
self
should: [ LanguageRange fromString: 'en-L123' ]
raise: InstanceCreationFailed
withMessageText: 'ISO 15924 script codes must be 4 letters.'
]

range := LanguageRange from: #('en' 'Latn' 'us').
self
assert: range subtags equals: #('en' 'Latn' 'US');
assert: range printString equals: 'en-Latn-US'
{ #category : 'tests' }
LanguageRangeTest >> testCantCreateWithEmptyString [

self
should: [ LanguageRange fromString: '' ]
raise: InstanceCreationFailed
withMessageText: 'At least one sub tag is required.'
]

{ #category : 'tests' }
LanguageRangeTest >> testMatches [
LanguageRangeTest >> testCantCreateWithoutSubtags [

| range |
self
should: [ LanguageRange composedOf: #( ) ]
raise: InstanceCreationFailed
withMessageText: 'At least one sub tag is required.'
]

range := LanguageRange from: #('en' 'US').
self
deny: ( range matches: 'en-Latn-US' asLanguageTag );
assert: ( range matches: 'en-US' asLanguageTag );
deny: ( range matches: 'en' asLanguageTag );
assert: ( range matches: 'en-us-x-x-x' asLanguageTag ).
{ #category : 'tests' }
LanguageRangeTest >> testComparison [

range := LanguageRange from: #('en').
self
assert: ( range matches: 'en-Latn-US' asLanguageTag );
assert: ( range matches: 'en-US' asLanguageTag );
assert: ( range matches: 'en' asLanguageTag );
assert: ( range matches: 'en-us-x-x-x' asLanguageTag )
| range |
range := LanguageRange composedOf: #( 'en' 'US' ).
self
assert: range equals: ( LanguageRange fromString: 'en-us' );
assert: range hash equals: ( LanguageRange fromString: 'en-us' ) hash;
deny: range equals: LanguageRange any;
deny: range equals: ( LanguageRange composedOf: #( 'en' ) )
]

{ #category : 'tests' }
LanguageRangeTest >> testCreation [

| range |
range := LanguageRange composedOf: #( 'en' 'Latn' 'us' ).
self
assert: range subtags equals: #( 'en' 'Latn' 'US' );
assert: range printString equals: 'en-Latn-US'
]

{ #category : 'tests' }
LanguageRangeTest >> testMatches [

| range |
range := LanguageRange composedOf: #( 'en' 'US' ).
self
deny: ( range matches: 'en-Latn-US' asLanguageTag );
assert: ( range matches: 'en-US' asLanguageTag );
deny: ( range matches: 'en' asLanguageTag );
assert: ( range matches: 'en-us-x-x-x' asLanguageTag );
deny: ( range matches: 'en-CA' asLanguageTag ).

range := LanguageRange composedOf: #( 'en' ).
self
assert: ( range matches: 'en-Latn-US' asLanguageTag );
assert: ( range matches: 'en-US' asLanguageTag );
assert: ( range matches: 'en-CA' asLanguageTag );
assert: ( range matches: 'en' asLanguageTag );
assert: ( range matches: 'en-us-x-x-x' asLanguageTag );
deny: ( range matches: 'es' asLanguageTag )
]

{ #category : 'tests' }
Expand Down
24 changes: 21 additions & 3 deletions source/Buoy-Localization-Tests/LanguageTagTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,33 @@ LanguageTagTest >> testCantCreateWhenScriptIsInvalid [
withMessageText: 'ISO 15924 script codes must be 4 letters.'
]

{ #category : 'tests' }
LanguageTagTest >> testCantCreateWithEmptyString [

self
should: [ LanguageTag fromString: '' ]
raise: InstanceCreationFailed
withMessageText: 'At least one sub tag is required.'
]

{ #category : 'tests' }
LanguageTagTest >> testCantCreateWithoutSubtags [

self
should: [ LanguageTag composedOf: #( ) ]
raise: InstanceCreationFailed
withMessageText: 'At least one sub tag is required.'
]

{ #category : 'tests' }
LanguageTagTest >> testComparison [

| tag |
tag := LanguageTag from: #( 'en' 'US' ).
tag := LanguageTag composedOf: #( 'en' 'US' ).
self
assert: tag equals: ( LanguageTag fromString: 'en-us' );
assert: tag hash equals: ( LanguageTag fromString: 'en-us' ) hash;
deny: tag equals: ( LanguageTag from: #( 'en' ) )
deny: tag equals: ( LanguageTag composedOf: #( 'en' ) )
]

{ #category : 'tests' }
Expand Down Expand Up @@ -139,7 +157,7 @@ LanguageTagTest >> testCreationWithScriptButNoRegion [
LanguageTagTest >> testPrintString [

| tag |
tag := LanguageTag from: #( 'en' 'US' ).
tag := LanguageTag composedOf: #( 'en' 'US' ).
self
assert: tag asString equals: 'en-US';
assert: tag printString equals: 'en-US'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ StringLocalizationExtensionsTest >> assertUnescaping: string raisesAsError: erro
]

{ #category : 'tests - escaping' }
StringLocalizationExtensionsTest >> testErrorsInUnescaped [
StringLocalizationExtensionsTest >> testErrorsWhenUnescaping [

self assertUnescaping: '\💩' raisesAsError: 'There''s no escaping rule for "💩" (\u{1F4A9})'.

Expand Down Expand Up @@ -89,7 +89,7 @@ StringLocalizationExtensionsTest >> testEscapedUnicode [
]

{ #category : 'tests - escaping' }
StringLocalizationExtensionsTest >> testEscapedWithoutEscapingSequence [
StringLocalizationExtensionsTest >> testEscapedWhenThereIsNoEscapingSequence [

self
assert: 'aabb' escaped equals: 'aabb';
Expand Down Expand Up @@ -236,7 +236,7 @@ StringLocalizationExtensionsTest >> testUnescapedUnicode [
]

{ #category : 'tests - escaping' }
StringLocalizationExtensionsTest >> testUnescapedWithoutEscapingSequence [
StringLocalizationExtensionsTest >> testUnescapedWhenThereIsNoEscapingSequence [

self
assert: 'aabb' unescaped equals: 'aabb';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ControlCharactersEscapingRule >> priority [
ControlCharactersEscapingRule >> raiseError [

<ignoreForCoverage>
self error: 'The control character escaping rule is mishandling some case'
self error: 'The control character escaping rule is mishandling some case'
]

{ #category : 'escaping' }
Expand Down
Loading

0 comments on commit 33bd6ea

Please sign in to comment.