JavaCompatibleTest.scala 4.3 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1
/*
2
 * Copyright (c) 2022 bitlap
梦境迷离's avatar
梦境迷离 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

梦境迷离's avatar
梦境迷离 已提交
22
package org.bitlap.tools
梦境迷离's avatar
梦境迷离 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/**
 * @author 梦境迷离
 * @since 2021/11/23
 * @version 1.0
 */
class JavaCompatibleTest extends AnyFlatSpec with Matchers {

  "JavaCompatible1" should "ok" in {
    """
36
      |    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
37 38 39 40 41 42 43 44
      |    case class A(a: Int, b: Short, c: Byte, d: Double, e: Float, f: Long, g: Char, h: Boolean)
      |    val t = new A()
      |    assert(t.a == 0 && t.g == '?')
      |""".stripMargin should compile
  }

  "JavaCompatible2" should "ok" in {
    """
45
      |    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
46 47 48 49 50 51 52 53
      |    case class A(a: Int, b: Short, c: Byte, d: Double)(val e: Float, val f: Long)(val g: Char, val h: Boolean)
      |    val t = new A()
      |    assert(t.a == 0 && t.g == '?')
      |""".stripMargin should compile
  }

  "JavaCompatible3" should "failed" in {
    """
54
      |    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
55 56 57 58 59 60
      |    case class A(a: Int, b: Short, c: Byte, d: Double)(val e: Float, val f: Long)(g: Char, h: Boolean)
      |    val t = new A()
      |    assert(t.a == 0 && t.g == '?')
      |""".stripMargin shouldNot compile

    """
61
      |    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
62 63 64 65 66 67
      |    class A(val a: Int, val b: Short)
      |    val t = new A()
      |    assert(t.a == 0)
      |""".stripMargin shouldNot compile
  }

68 69
  "javaCompatible4" should "ok" in {
    @javaCompatible
I
IceMimosa 已提交
70
    case class A(var a: Int, b: Short, c: Byte, d: Double, e: Float, f: Long, g: Char, h: Boolean, i: String)
梦境迷离's avatar
梦境迷离 已提交
71 72
    val t = new A()
    assert(t.a == 0 && t.g == '?')
I
IceMimosa 已提交
73 74
    t.setA(10)
    assert(t.a == 10)
梦境迷离's avatar
梦境迷离 已提交
75 76 77 78
  }

  "JavaCompatible5" should "ok" in {
    import scala.beans.BeanProperty
79
    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
80 81 82 83 84 85 86
    case class A(@BeanProperty a: Int, b: Short, c: Byte, d: Double, e: Float, f: Long, g: Char, h: Boolean, i: String)
    val t = new A()
    assert(t.a == 0 && t.g == '?')
  }

  "JavaCompatible6" should "ok when exists @BeanProperty" in {
    import scala.beans.BeanProperty
87
    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
88 89 90 91 92 93 94 95 96
    case class A(@BeanProperty a: Int, b: Short, c: Byte, d: Double, e: Float, f: Long, g: Char, h: Boolean, i: String)
    val t = new A()
    assert(t.getA == 0)
    assert(t.getB == 0)
  }

  "JavaCompatible7" should "ok when exists super" in {
    import scala.beans.BeanProperty
    class B(@BeanProperty val name: String, @BeanProperty val id: Int)
97
    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
98 99 100 101 102 103 104 105 106 107
    case class A(a: Int, b: Short, override val name: String, override val id: Int) extends B(name, id)
    val t = new A()
    assert(t.getA == 0)
    assert(t.getB == 0)
  }

  // Why this code compile failed but test in """ """.stripMargin will pass?
  "JavaCompatible8" should "fail when exists super but not use @BeanProperty" in {
    """
      |    class B(val name: String, val id: Int)
108
      |    @javaCompatible
梦境迷离's avatar
梦境迷离 已提交
109 110 111 112
      |    case class A(a: Int, b: Short, override val name: String, override val id: Int) extends B(name, id)
      |    val t = new A()
      |""".stripMargin should compile
  }
梦境迷离's avatar
梦境迷离 已提交
113 114 115 116 117 118 119 120 121 122 123 124

  "JavaCompatible9" should "failed when exists private field" in {
    """
      | @javaCompatible
      | case class A(private val a: Int, b: Short)
      |""".stripMargin shouldNot compile

    """
      | @javaCompatible
      | case class B(private[this] val a: Int, b: Short)
      |""".stripMargin shouldNot compile
  }
梦境迷离's avatar
梦境迷离 已提交
125
}