ApplyTest.scala 1.9 KB
Newer Older
1 2
package io.github.dreamylost

S
Scala Steward 已提交
3 4
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
5 6 7 8 9 10 11

/**
 *
 * @author 梦境迷离
 * @since 2021/6/30
 * @version 1.0
 */
S
Scala Steward 已提交
12
class ApplyTest extends AnyFlatSpec with Matchers {
13 14 15 16 17 18 19 20 21

  "apply1" should "ok at class" in {
    // int: Int => private[this] val int: Int = _;
    // val j: Int => val j: Int = _;
    // apply => def apply(int: Int, j: Int, k: Option[String] = None, t: Option[Long] = Some(1L)): A = new A(int, j, k, t)

    """@toString @apply class A(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))""" should compile
    @toString
    @apply class A2(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))
梦境迷离's avatar
梦境迷离 已提交
22
    println(A2(1, 2, None, None))
23 24 25 26

    """@apply @toString class B(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))""" should compile
    @apply
    @toString class B2(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))
梦境迷离's avatar
梦境迷离 已提交
27
    println(B2(1, 2, None, None))
28 29 30 31 32 33

    // exists object
    """@apply @toString class B(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L));object B3""" should compile
    @apply
    @toString class B3(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))
    object B3
梦境迷离's avatar
梦境迷离 已提交
34
    println(B3(1, 2, None, None))
35 36 37 38 39
  }
  "apply2" should "failed at class" in {
    // FAILED, not support currying!!
    """@apply @toString class C(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))(o: Int = 1)""" shouldNot compile
  }
梦境迷离's avatar
梦境迷离 已提交
40

41
  "apply3" should "ok with currying" in {
梦境迷离's avatar
梦境迷离 已提交
42 43 44
    @apply
    @toString class B3(int: Int)(val j: Int)(var k: Option[String] = None)(t: Option[Long] = Some(1L))
  }
45 46 47 48 49 50 51 52 53 54 55

  "apply4" should "ok with generic" in {
    @apply
    @toString class B3[T, U](int: T)(val j: U)
    println(B3(1)(2))

    @toString
    @apply class B4[T, U](int: T, val j: U)
    println(B4("helloworld", 2))

  }
56
}