-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(random): enable random feature for primitives types
* feat(random): add failing test for random string * feat(random): add new feature configuration option * feat(random): enable feature for test features * feat(random): generate random string * feat(random): generate random string at runtime to avoid duplicatiopn * feat(random): generate random number * feat(random): extract length and number max value * feat(random): generate random boolean * feat(random): improve string random test * feat(random): add documentation * feat(random): add random module so it will be easier to read the code * feat(random): add random module so it will be easier to read the code * feat(random): add random module so it will be easier to read the code * revert playground changes * remove unnecessary length parameter * add min and max constant to improve readability * use existing propertyName string getter * ensure number math.random doesnt returns value outside the default range 0,1
- Loading branch information
Showing
24 changed files
with
243 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const karmaBaseConfig = require('./karma.config.base'); | ||
|
||
module.exports = function(config) { | ||
const karmaConfig = karmaBaseConfig(config, '../../test/features/context.ts'); | ||
|
||
config.set(karmaConfig); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const ProcessService = require('../../utils/process/process'); | ||
|
||
function DetermineFeaturesFromEnvironment() { | ||
const processService = ProcessService(process); | ||
const features = processService.getEnvironmentValue('FEATURES'); | ||
|
||
if (features) { | ||
return [ | ||
'random' | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
module.exports = DetermineFeaturesFromEnvironment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { TsAutoMockOptions } from './options'; | ||
|
||
export const defaultOptions: TsAutoMockOptions = { | ||
features: [], | ||
debug: false, | ||
cacheBetweenTests: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TsAutoMockFeaturesOption = 'random'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { GetOptionByKey } from './options'; | ||
|
||
export function IsTsAutoMockRandomEnabled(): boolean { | ||
return GetOptionByKey('features').includes('random'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Random as ɵRandom } from './random'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const MIN_NUMBER: number = -10000; | ||
const MAX_NUMBER: number = 10000; | ||
|
||
export class Random { | ||
public static number(): number { | ||
return Math.random() * (MAX_NUMBER - MIN_NUMBER) + MIN_NUMBER; | ||
} | ||
|
||
public static string(prefix: string): string { | ||
return prefix + Math.random().toString(20).substr(2, 6); | ||
} | ||
|
||
public static boolean(): boolean { | ||
return !Math.round(Math.random()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import * as ts from 'typescript'; | ||
import { IsTsAutoMockRandomEnabled } from '../../../options/random'; | ||
import { RandomPropertyAccessor } from '../random/random'; | ||
import { GetBooleanFalseDescriptor } from './booleanFalse'; | ||
|
||
export function GetBooleanDescriptor(): ts.Expression { | ||
if (IsTsAutoMockRandomEnabled()) { | ||
return ts.createCall(RandomPropertyAccessor('boolean'), [], []); | ||
} | ||
return GetBooleanFalseDescriptor(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import * as ts from 'typescript'; | ||
import { IsTsAutoMockRandomEnabled } from '../../../options/random'; | ||
import { RandomPropertyAccessor } from '../random/random'; | ||
|
||
export function GetNumberDescriptor(): ts.Expression { | ||
if (IsTsAutoMockRandomEnabled()) { | ||
return ts.createCall(RandomPropertyAccessor('number'), [], []); | ||
} | ||
return ts.createLiteral(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as ts from 'typescript'; | ||
import { MockDefiner } from '../../mockDefiner/mockDefiner'; | ||
import { ModuleName } from '../../mockDefiner/modules/moduleName'; | ||
import { PrivateIdentifier } from '../../privateIdentifier/privateIdentifier'; | ||
|
||
export function RandomPropertyAccessor(methodName: string): ts.PropertyAccessExpression { | ||
return ts.createPropertyAccess( | ||
ts.createPropertyAccess( | ||
MockDefiner.instance.getCurrentModuleIdentifier(ModuleName.Random), | ||
PrivateIdentifier('Random') | ||
), | ||
ts.createIdentifier(methodName), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import * as ts from 'typescript'; | ||
import { IsTsAutoMockRandomEnabled } from '../../../options/random'; | ||
import { PropertySignatureCache } from '../property/cache'; | ||
import { RandomPropertyAccessor } from '../random/random'; | ||
import { TypescriptHelper } from '../helper/helper'; | ||
|
||
export function GetStringDescriptor(): ts.Expression { | ||
if (IsTsAutoMockRandomEnabled()) { | ||
const propertyName: string = TypescriptHelper.GetStringPropertyName(PropertySignatureCache.instance.get()); | ||
const prefix: ts.StringLiteral = ts.createLiteral(propertyName); | ||
return ts.createCall(RandomPropertyAccessor('string'), [], [prefix]); | ||
} | ||
return ts.createLiteral(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/typedef | ||
const frameworkContext = require.context('./', true, /\.test\.ts$/); | ||
frameworkContext.keys().map(frameworkContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('Random boolean', () => { | ||
it('should be generated by math random', () => { | ||
type WithBoolean = { | ||
propertyName: boolean; | ||
}; | ||
|
||
const spy: jasmine.Spy = spyOn(Math, 'random'); | ||
|
||
spy.and.returnValue(0); | ||
|
||
const mock: WithBoolean = createMock<WithBoolean>(); | ||
|
||
// because we are mocking the return value of Math.random with 0 the result should be always !0 = true | ||
expect(mock.propertyName).toBe(true); | ||
|
||
spy.calls.reset(); | ||
spy.and.returnValue(1); | ||
|
||
const mock2: WithBoolean = createMock<WithBoolean>(); | ||
// because we are mocking the return value of Math.random with 1 the result should be always !1 = false | ||
expect(mock2.propertyName).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('Random number', () => { | ||
it('should be generated by math random', () => { | ||
type WithNumber = { | ||
propertyName: number; | ||
}; | ||
|
||
const spy: jasmine.Spy = spyOn(Math, 'random'); | ||
|
||
spy.and.returnValue(1); | ||
|
||
const mock: WithNumber = createMock<WithNumber>(); | ||
|
||
// because we are mocking the return value of Math.random with 1 the result should be = 1 * (10000 - -10000) + -10000 | ||
expect(mock.propertyName).toBe(10000); | ||
|
||
spy.calls.reset(); | ||
spy.and.returnValue(0); | ||
|
||
const mock2: WithNumber = createMock<WithNumber>(); | ||
|
||
// because we are mocking the return value of Math.random with 10 the result should be = 10 * (10000 - -10000) + -10000 | ||
expect(mock2.propertyName).toBe(-10000); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('Random string', () => { | ||
it('should return a random string value with the property name and 6 character', () => { | ||
interface WithString { | ||
prop: string; | ||
} | ||
|
||
const mock: WithString = createMock<WithString>(); | ||
|
||
expect(mock.prop).toMatch(/prop.{6}/); | ||
}); | ||
|
||
it('should include the property name at the beginning of the value', () => { | ||
interface WithString { | ||
propertyName: string; | ||
} | ||
|
||
const mock: WithString = createMock<WithString>(); | ||
|
||
expect(mock.propertyName).toMatch(/propertyName.*/); | ||
}); | ||
|
||
it('should include the function name when the string is the result of the function', () => { | ||
type WithString = { | ||
fnReturnsString: () => string; | ||
}; | ||
|
||
const mock: WithString = createMock<WithString>(); | ||
|
||
expect(mock.fnReturnsString()).toMatch(/fnReturnsString.*/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters