Commit 76118ee0 by 李楚霏

重构第一步

parents
export class Length {
value
unit
unitEnum = ['foot', 'yard', 'inch']
constructor(val, uint) {
this.value = val
this.unit = uint
}
getVal() {
return this.value
}
getUint() {
return this.unit
}
parseTo(u) {
let len = this
if (this.unit === 'yard') {
if (u === 'f') {
len = new Length(this.value * 3, u)
} else if (u === 'inch') {
len = new Length(this.value * 36, u)
}
}
if (this.unit === 'inch') {
if (u === 'yard') {
len = new Length(this.value / 36, u)
} else if (u === 'f') {
len = new Length(this.value / 12, u)
}
}
if (this.unit === 'f') {
if (u === 'yard') {
len = new Length(this.value / 3, u)
} else if (u === 'inch') {
len = new Length(this.value * 12, u)
}
}
return len
}
}
import { Length } from './index'
describe('Length', () => {
it("should 1 'foot' equals 1 'foot'", () => {
const length = new Length(1, 'f')
expect(length.getVal()).toEqual(1)
expect(length.getUint()).toEqual('f')
})
it("should 1 'foot' equals 12 inches", () => {
const result = new Length(1, 'f').parseTo('inch')
expect(result.getVal()).toEqual(12)
expect(result.getUint()).toEqual('inch')
})
it("should 3 feet equals 1 'yard'", () => {
const result = new Length(3, 'f').parseTo('yard')
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
})
it("should 1 'yard' equals 3 feet", () => {
const result = new Length(1, 'yard').parseTo('f')
expect(result.getVal()).toEqual(3)
expect(result.getUint()).toEqual('f')
})
it("should 1 'yard' equals 36 inches", () => {
const result = new Length(1, 'yard').parseTo('inch')
expect(result.getVal()).toEqual(36)
expect(result.getUint()).toEqual('inch')
})
it("should 1 'yard' equals 1 'yard'", () => {
const result = new Length(1, 'yard').parseTo('yard')
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
})
it("should 12 inches equals 1 'foot'", () => {
const result = new Length(12, 'inch').parseTo('f')
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('f')
})
it("should 36 inches equals 1 'yard'", () => {
const result = new Length(36, 'inch').parseTo('yard')
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
})
it("should 1 inch equals 1 'inch'", () => {
const result = new Length(1, 'inch').parseTo('inch')
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('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