Commit b4165299 authored by Lord of the dance's avatar Lord of the dance Committed by smartcontracts

test: adds tests and docstrings for core-utils misc functions

parent ed234a8d
---
'@eth-optimism/core-utils': patch
---
Added tests and docstrings to misc functions
...@@ -11,7 +11,12 @@ export const sleep = async (ms: number): Promise<void> => { ...@@ -11,7 +11,12 @@ export const sleep = async (ms: number): Promise<void> => {
}) })
} }
// Returns a copy of an object /**
* Returns a clone of the object.
*
* @param obj Object to clone.
* @returns Clone of the object.
*/
export const clone = (obj: any): any => { export const clone = (obj: any): any => {
if (typeof obj === 'undefined') { if (typeof obj === 'undefined') {
throw new Error(`Trying to clone undefined object`) throw new Error(`Trying to clone undefined object`)
...@@ -33,6 +38,13 @@ export const reqenv = (name: string): string => { ...@@ -33,6 +38,13 @@ export const reqenv = (name: string): string => {
return value return value
} }
/**
* Loads a variable from the environment and returns a fallback if not found.
*
* @param name Name of the variable to load.
* @param [fallback] Optional value to be returned as fallback.
* @returns Value of the variable as a string, fallback or undefined.
*/
export const getenv = (name: string, fallback?: string): string | undefined => { export const getenv = (name: string, fallback?: string): string | undefined => {
return process.env[name] || fallback return process.env[name] || fallback
} }
/* Imports: Internal */ /* Imports: Internal */
import { expect } from './setup' import { expect } from './setup'
import { sleep } from '../src' import { sleep, clone, reqenv, getenv } from '../src'
describe('sleep', async () => { describe('sleep', async () => {
it('should return wait input amount of ms', async () => { it('should return wait input amount of ms', async () => {
...@@ -10,3 +10,83 @@ describe('sleep', async () => { ...@@ -10,3 +10,83 @@ describe('sleep', async () => {
expect(startTime + 1000 <= endTime).to.deep.equal(true) expect(startTime + 1000 <= endTime).to.deep.equal(true)
}) })
}) })
describe('clone', async () => {
it('should return a cloned object', async () => {
const exampleObject = { example: 'Example' }
const clonedObject = clone(exampleObject)
expect(clonedObject).to.not.equal(exampleObject)
expect(JSON.stringify(clonedObject)).to.equal(JSON.stringify(exampleObject))
})
})
describe('reqenv', async () => {
let cachedEnvironment
const temporaryEnvironmentKey = 'testVariable'
const temporaryEnvironment = {
[temporaryEnvironmentKey]: 'This is an environment variable',
}
before(() => {
cachedEnvironment = process.env
process.env = temporaryEnvironment
})
it('should return an existent environment variable', async () => {
const requiredEnvironmentValue = reqenv(temporaryEnvironmentKey)
expect(requiredEnvironmentValue).to.equal(
temporaryEnvironment[temporaryEnvironmentKey]
)
})
it('should throw an error trying to return a variable that does not exist', async () => {
const undeclaredVariableName = 'undeclaredVariable'
const failedReqenv = () => reqenv(undeclaredVariableName)
expect(failedReqenv).to.throw()
})
after(() => {
process.env = cachedEnvironment
})
})
describe('getenv', async () => {
let cachedEnvironment
const temporaryEnvironmentKey = 'testVariable'
const temporaryEnvironment = {
[temporaryEnvironmentKey]: 'This is an environment variable',
}
const fallback = 'fallback'
before(() => {
cachedEnvironment = process.env
process.env = temporaryEnvironment
})
it('should return an existent environment variable', async () => {
const environmentVariable = getenv(temporaryEnvironmentKey)
expect(environmentVariable).to.equal(
temporaryEnvironment[temporaryEnvironmentKey]
)
})
it('should return an existent environment variable even if fallback is passed', async () => {
const environmentVariable = getenv(temporaryEnvironmentKey, fallback)
expect(environmentVariable).to.equal(
temporaryEnvironment[temporaryEnvironmentKey]
)
})
it('should return fallback if variable is not defined', async () => {
const undeclaredVariableName = 'undeclaredVariable'
expect(getenv(undeclaredVariableName, fallback)).to.equal(fallback)
})
it('should return undefined if no fallback is passed and variable is not defined', async () => {
expect(getenv('undeclaredVariable')).to.be.undefined
})
after(() => {
process.env = cachedEnvironment
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment