1+ /*
2+ Copyright (c) 2025, Oracle and/or its affiliates.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ https://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+ import { expect } from 'chai' ;
17+ import { describe , it , beforeEach , before , after } from "mocha" ;
18+ import * as sinon from 'sinon' ;
19+ import * as vscode from 'vscode' ;
20+ import * as assert from 'assert' ;
21+ import { Extension } from 'vscode' ;
22+ import { LOGGER } from '../../../logger' ;
23+ import { mock , instance , when , reset } from "ts-mockito" ;
24+
25+ import * as path from 'path' ;
26+
27+ describe ( 'localiser tests' , ( ) => {
28+ let loggerLogStub : sinon . SinonStub ;
29+ let mockedExtns : typeof vscode . extensions ;
30+ let extMock : Extension < any > ;
31+ let mockedEnv : typeof vscode . env ;
32+ let mockedL10n : typeof vscode . l10n ;
33+ let currentDir = __dirname . replace ( "/out/" , "/src/" ) ;
34+
35+ before ( ( ) => {
36+ let vscodeObj = ( vscode as typeof vscode & { mockedExtns : typeof vscode . extensions , mockedEnv : typeof vscode . env , mockedL10n : typeof vscode . l10n } ) ;
37+ mockedExtns = vscodeObj . mockedExtns ;
38+ mockedEnv = vscodeObj . mockedEnv ;
39+ mockedL10n = vscodeObj . mockedL10n ;
40+ extMock = mock < Extension < any > > ( ) ;
41+ loggerLogStub = sinon . stub ( LOGGER , "error" ) ;
42+ } ) ;
43+ after ( ( ) => {
44+ sinon . reset ( ) ;
45+ reset ( mockedExtns ) ;
46+ reset ( extMock ) ;
47+ reset ( mockedEnv ) ;
48+ reset ( mockedL10n ) ;
49+ } ) ;
50+
51+ beforeEach ( ( ) => {
52+ sinon . reset ( ) ;
53+ reset ( mockedExtns ) ;
54+ reset ( extMock ) ;
55+ reset ( mockedEnv ) ;
56+ reset ( mockedL10n ) ;
57+ } )
58+
59+
60+
61+ describe ( 'l10n tests' , ( ) => {
62+ describe ( 'issue while reading bundle' , ( ) => {
63+ it ( 'file not found error' , ( ) => {
64+ let msg : string | null = null ;
65+ when ( extMock ?. extensionPath ) . thenReturn ( path . join ( currentDir , 'doesnt-exist' ) ) ;
66+ var mkInst = instance ( extMock ) ;
67+ when ( mockedExtns . getExtension ( "oracle.oracle-java" ) ) . thenReturn ( mkInst ) ;
68+ try {
69+ require ( '../../../localiser' ) ;
70+ } catch ( e ) {
71+ msg = ( e as any & { message : string } ) . message
72+ }
73+ assert . strictEqual ( msg ! ! . includes ( "no such file or directory" ) , true ) ;
74+ expect ( loggerLogStub . called ) . to . be . true ;
75+ } ) ;
76+ it ( 'file parsing error' , ( ) => {
77+ let msg : string | null = null ;
78+ when ( extMock ?. extensionPath ) . thenReturn ( path . join ( currentDir , 'resources' , 'corrupt' ) ) ;
79+ var mkInst = instance ( extMock ) ;
80+ when ( mockedExtns . getExtension ( "oracle.oracle-java" ) ) . thenReturn ( mkInst ) ;
81+ try {
82+ require ( '../../../localiser' ) ;
83+ } catch ( e ) {
84+ msg = ( e as any & { message : string } ) . message
85+ }
86+ assert . strictEqual ( msg ! ! . includes ( "Bad control character in string literal in JSON" ) , true ) ;
87+ expect ( loggerLogStub . called ) . to . be . true ;
88+ } ) ;
89+
90+ } ) ;
91+ describe ( 'l10n initialisation tests' , ( ) => {
92+ it ( 'l10n initialized' , ( ) => {
93+ when ( extMock ?. extensionPath ) . thenReturn ( path . join ( currentDir , 'resources' ) ) ;
94+ var mkInst = instance ( extMock ) ;
95+ when ( mockedExtns . getExtension ( "oracle.oracle-java" ) ) . thenReturn ( mkInst ) ;
96+ require ( '../../../localiser' ) ;
97+ expect ( loggerLogStub . called ) . to . be . false ;
98+ } ) ;
99+ it ( 'get nbLocaleCode and get value' , ( ) => {
100+ when ( extMock ?. extensionPath ) . thenReturn ( path . join ( currentDir , 'resources' ) ) ;
101+ var mkExtInst = instance ( extMock ) ;
102+ when ( mockedEnv . language ) . thenReturn ( "en" ) ;
103+ when ( mockedExtns . getExtension ( "oracle.oracle-java" ) ) . thenReturn ( mkExtInst ) ;
104+ when ( mockedL10n . bundle ) . thenReturn ( undefined ) ;
105+ let l10n = require ( '../../../localiser' ) ;
106+ let l10nObj = l10n . l10n as { nbLocaleCode ( ) : string , value ( key : string , placeholderMap ?: Record < string , any > ) : string } ;
107+ assert . strictEqual ( l10nObj . nbLocaleCode ( ) , "en" ) ;
108+ assert . strictEqual ( l10nObj . value ( "label1" ) , "label1 description" ) ;
109+ assert . strictEqual ( l10nObj . value ( "label2" , { "placeholder1" : "sample data" } ) , "lable2 sample data description" ) ;
110+ expect ( loggerLogStub . called ) . to . be . false ;
111+ } ) ;
112+ } ) ;
113+ } ) ;
114+ } ) ;
0 commit comments