Commit 11022629 by 李楚霏

task4--改变函数参数

parent afd3bfcb
import * as Constants from './constants'
export class Length {
value
unit
static FOOT = 'foot'
static YARD = 'yard'
static INCH = 'inch'
constructor(val, uint) {
this.value = val
......@@ -19,26 +17,26 @@ export class Length {
}
parseTo(unitNode) {
let unitLength = this
if (this.unit === Length.YARD ) {
if (unitNode === Length.FOOT) {
if (this.unit === Constants.Constants.YARD) {
if (unitNode === Constants.Constants.FOOT) {
unitLength = new Length(this.value * 3, unitNode)
} else if (unitNode === Length.INCH) {
} else if (unitNode === Constants.Constants.INCH) {
unitLength = new Length(this.value * 36, unitNode)
}
}
if (this.unit === Length.INCH) {
if (unitNode === Length.YARD) {
if (this.unit === Constants.Constants.INCH) {
if (unitNode === Constants.Constants.YARD) {
unitLength = new Length(this.value / 36, unitNode)
} else if (unitNode === Length.FOOT) {
} else if (unitNode === Constants.Constants.FOOT) {
unitLength = new Length(this.value / 12, unitNode)
}
}
if (this.unit === Length.FOOT) {
if (unitNode === Length.YARD) {
if (this.unit === Constants.Constants.FOOT) {
if (unitNode === Constants.Constants.YARD) {
unitLength = new Length(this.value / 3, unitNode)
} else if (unitNode === Length.INCH) {
} else if (unitNode === Constants.Constants.INCH) {
unitLength = new Length(this.value * 12, unitNode)
}
}
......
import { Length } from './index'
import {Constants} from './constants'
describe('Length', () => {
it("should 1 'foot' equals 1 'foot'", () => {
const length = new Length(1, Length.FOOT)
const length = new Length(1, Constants.FOOT)
expect(length.getVal()).toEqual(1)
expect(length.getUint()).toEqual(Length.FOOT)
expect(length.getUint()).toEqual(Constants.FOOT)
})
it("should 1 'foot' equals 12 inches", () => {
const result = new Length(1, Length.FOOT).parseTo(Length.INCH)
const result = new Length(1, Constants.FOOT).parseTo(Constants.INCH)
expect(result.getVal()).toEqual(12)
expect(result.getUint()).toEqual(Length.INCH)
expect(result.getUint()).toEqual(Constants.INCH)
})
it("should 3 feet equals 1 Length.YARD", () => {
const result = new Length(3, Length.FOOT).parseTo(Length.YARD)
it("should 3 feet equals 1 yard", () => {
const result = new Length(3, Constants.FOOT).parseTo(Constants.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual(Length.YARD)
expect(result.getUint()).toEqual(Constants.YARD)
})
it("should 1 Length.YARD equals 3 feet", () => {
const result = new Length(1, Length.YARD).parseTo(Length.FOOT)
it("should 1 Constants.YARD equals 3 feet", () => {
const result = new Length(1, Constants.YARD).parseTo(Constants.FOOT)
expect(result.getVal()).toEqual(3)
expect(result.getUint()).toEqual(Length.FOOT)
expect(result.getUint()).toEqual(Constants.FOOT)
})
it("should 1 Length.YARD equals 36 inches", () => {
const result = new Length(1, Length.YARD).parseTo(Length.INCH)
it("should 1 Constants.YARD equals 36 inches", () => {
const result = new Length(1, Constants.YARD).parseTo(Constants.INCH)
expect(result.getVal()).toEqual(36)
expect(result.getUint()).toEqual(Length.INCH)
expect(result.getUint()).toEqual(Constants.INCH)
})
it("should 1 Length.YARD equals 1 Length.YARD", () => {
const result = new Length(1, Length.YARD).parseTo(Length.YARD)
it("should 1 Constants.YARD equals 1 Constants.YARD", () => {
const result = new Length(1, Constants.YARD).parseTo(Constants.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual(Length.YARD)
expect(result.getUint()).toEqual(Constants.YARD)
})
it("should 12 inches equals 1 'foot'", () => {
const result = new Length(12, Length.INCH).parseTo(Length.FOOT)
const result = new Length(12, Constants.INCH).parseTo(Constants.FOOT)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual(Length.FOOT)
expect(result.getUint()).toEqual(Constants.FOOT)
})
it("should 36 inches equals 1 yard", () => {
const result = new Length(36, Length.INCH).parseTo(Length.YARD)
const result = new Length(36, Constants.INCH).parseTo(Constants.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual(Length.YARD)
expect(result.getUint()).toEqual(Constants.YARD)
})
it("should 1 inch equals 1 inch", () => {
const result = new Length(1, Length.INCH).parseTo(Length.INCH)
const result = new Length(1, Constants.INCH).parseTo(Constants.INCH)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual(Length.INCH)
expect(result.getUint()).toEqual(Constants.INCH)
})
})
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 sign in to comment