mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-11 12:12:55 +00:00
refactor: simpify translator class functions
This commit is contained in:
committed by
Jovan Krunić
parent
27417e80e1
commit
cf83692e71
@@ -13,11 +13,12 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {expect} from 'chai';
|
||||
import clone = require('fast-clone');
|
||||
import {slow, suite, test, timeout} from 'mocha-typescript';
|
||||
|
||||
import {SCThingOriginType, SCThingType} from '../src/core/Thing';
|
||||
import {SCBuildingWithoutReferences} from '../src/core/things/Building';
|
||||
import {SCDish, SCDishMeta} from '../src/core/things/Dish';
|
||||
import {SCPerson} from '../src/core/things/Person';
|
||||
import {SCThingTranslator} from '../src/core/Translator';
|
||||
|
||||
const building: SCBuildingWithoutReferences = {
|
||||
@@ -96,29 +97,15 @@ const dish: SCDish = {
|
||||
uid: '540862f3-ea30-5b8f-8678-56b4dc217140',
|
||||
};
|
||||
|
||||
const person: SCPerson = {
|
||||
familyName: 'base-familyName-name',
|
||||
givenName: 'base-givenName-name',
|
||||
homeLocations: [building, building, building],
|
||||
name : 'base-person-name',
|
||||
origin: {
|
||||
indexed: '1970',
|
||||
name: 'ding',
|
||||
type: SCThingOriginType.Remote,
|
||||
},
|
||||
type: SCThingType.Person,
|
||||
uid: '1234',
|
||||
};
|
||||
|
||||
const translator = new SCThingTranslator('de', 'en');
|
||||
const translator = new SCThingTranslator('de');
|
||||
// tslint:disable-next-line:no-eval
|
||||
const languageNonExistant = eval("'jp'");
|
||||
// this will simulate a translator always utilizing the base language translations
|
||||
const translatorWithFallback = new SCThingTranslator(languageNonExistant);
|
||||
|
||||
// tslint:disable:member-ordering TranslationSpec
|
||||
// tslint:disable:member-ordering TranslationSpecInplace
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class TranslationSpec {
|
||||
export class TranslationSpecInplace {
|
||||
|
||||
@test
|
||||
public directStringLiteralType() {
|
||||
@@ -233,135 +220,6 @@ export class TranslationSpec {
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable:member-ordering TranslationSpecByString
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class TranslationSpecByString {
|
||||
|
||||
@test
|
||||
public directStringLiteralType() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'type')).to.equal('Essen');
|
||||
}
|
||||
|
||||
@test
|
||||
public directStringProperty() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'name')).to.equal('de-dish-name');
|
||||
}
|
||||
|
||||
@test
|
||||
public directArrayOfString() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'characteristics')).to.deep
|
||||
.equal([{name: 'de-characteristic0'}, {name: 'de-characteristic1'}]);
|
||||
}
|
||||
|
||||
@test
|
||||
public directArrayOfStringSubscript() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'characteristics[1]'))
|
||||
.to.deep.equal({name: 'de-characteristic1'});
|
||||
}
|
||||
|
||||
@test
|
||||
public directMetaArrayOfString() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'categories')).to.deep.equal(['Hauptgericht', 'Nachtisch']);
|
||||
}
|
||||
|
||||
@test
|
||||
public directMetaArrayOfStringSubscript() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'categories[1]')).to.equal('Nachtisch');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedStringLiteralType() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'offers[0].inPlace.type')).to.equal('Gebäude');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedStringProperty() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'offers[0].inPlace.name')).to.equal('de-space-name');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedMetaArrayOfString() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'offers[0].inPlace.categories'))
|
||||
.to.deep.equal(['Büro', 'Bildung']);
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedMetaArrayOfStringSubscript() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'offers[0].inPlace.categories[1]')).to.equal('Bildung');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedArrayOfStringSubscript() {
|
||||
expect(translator.getFieldValueTranslation(dish, 'offers[0].inPlace.floors[1]')).to.equal('de-floor1');
|
||||
}
|
||||
|
||||
@test
|
||||
public directStringLiteralTypeFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'type')).to.equal('dish');
|
||||
}
|
||||
|
||||
@test
|
||||
public directStringPropertyFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'name')).to.equal('base-dish-name');
|
||||
}
|
||||
|
||||
@test
|
||||
public directArrayOfStringSubscriptFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'characteristics[1]'))
|
||||
.to.deep.equal({name: 'base-characteristic1'});
|
||||
}
|
||||
|
||||
@test
|
||||
public directMetaArrayOfStringFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'categories'))
|
||||
.to.deep.equal(['main dish', 'dessert']);
|
||||
}
|
||||
|
||||
@test
|
||||
public directMetaArrayOfStringSubscriptFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'categories[1]')).to.equal('dessert');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedStringLiteralTypeFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.type')).to.equal('building');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedStringPropertyFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.name')).to.equal('base-space-name');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedMetaArrayOfStringFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.categories'))
|
||||
.to.deep.equal(['office', 'education']);
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedMetaArrayOfStringSubscriptFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.categories[1]'))
|
||||
.to.equal('education');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedArrayOfStringSubscriptFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.floors[1]'))
|
||||
.to.equal('base-floor1');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedArrayOfStringSubscriptUncommonFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(dish, 'offers[0].inPlace.floors.1')).to.equal('base-floor1');
|
||||
}
|
||||
|
||||
@test
|
||||
public nestedNestedMetaArrayOfStringSubscriptUncommonFallback() {
|
||||
expect(translatorWithFallback.getFieldValueTranslation(person, 'homeLocations.1.categories.1'))
|
||||
.to.equal('education');
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable:member-ordering no-eval no-unused-expression TranslationSpec
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class MetaTranslationSpec {
|
||||
@@ -376,7 +234,7 @@ export class MetaTranslationSpec {
|
||||
|
||||
@test
|
||||
public thingWithoutMetaClass() {
|
||||
const dishCopy = Object.assign({}, dish);
|
||||
const dishCopy = clone(dish);
|
||||
const typeNonExistant = eval("(x) => x + 'typeNonExistant';");
|
||||
// this will assign a non existant SCThingType to dishCopy
|
||||
dishCopy.type = typeNonExistant();
|
||||
|
||||
Reference in New Issue
Block a user