请参阅我创建的以下单元测试:
import android.graphics.PointF
import org.junit.Assert.assertEquals
import org.junit.Test
class SimpleTest {
data class A(val x: Float, val y: Float) {
val origin = PointF(x, y)
val origin1 by lazy { PointF(x, y) }
}
val sut = A(10f, 10f)
@Test fun succeed() {
assertEquals(10f, sut.x)
assertEquals(10f, sut.y)
}
@Test fun failed() {
assertEquals(10f, sut.origin.x)
assertEquals(10f, sut.origin.y)
}
@Test fun failed1() {
assertEquals(10f, sut.origin1.x)
assertEquals(10f, sut.origin1.y)
}
}
第二个和第三个测试用例失败并出现错误:
java.lang.AssertionError:
Expected :10.0
Actual :0.0
显然,当用于初始化PointF
时,原始浮点数变为0,如果用于初始化RectF
,则同样适用。
这个错误彻底打动了我的脑海,这是Kotlin的一个错误吗?
更新:
我已经确认这三个测试用例是作为检测测试传递的,这证明了Android类的存根是导致测试失败的原因。但是,我仍然在我的应用程序中观察到同样的问题 - 这不是单元测试......
data class Viewport(val x: Float, val y: Float, val w: Int, val h: Int, val scale: Float) {
val origin = Point2D(x, y)
val bound = RectF(x, y, x + w * scale, y + h * scale)
....
}
在应用程序的某个地方,我有以下logcat语句:
Log.d(TAG, "viewport=$viewport, origin=${viewport!!.origin}, bound=${viewport!!.bound}")
我得到的logcat输出是:
01-05 12:53:17.376 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.412 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.431 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.448 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.468 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.487 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
01-05 12:53:17.501 10870-10870/com.quirklogic.ink D/Painter: viewport=Viewport(x=-292.81342, y=-39.189453, w=720, h=1280, scale=1.0), origin=Point2D(x=-292.81342, y=-39.189453), bound=RectF(0.0, 0.0, 720.0, 1280.0)
UPDATE2:
我发现确实是我自己的错,我改变了将其重置为(0,0)原点的界限。不是Kotlin的问题。