ApplyTest.scala 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2021 jxnu-liguobin && contributors
 *
 * 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.
 */

22 23
package io.github.dreamylost

S
Scala Steward 已提交
24 25
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
26 27 28 29 30 31 32

/**
 *
 * @author 梦境迷离
 * @since 2021/6/30
 * @version 1.0
 */
S
Scala Steward 已提交
33
class ApplyTest extends AnyFlatSpec with Matchers {
34 35 36 37 38 39 40 41 42

  "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
梦境迷离 已提交
43
    println(A2(1, 2, None, None))
44 45 46 47

    """@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
梦境迷离 已提交
48
    println(B2(1, 2, None, None))
49 50 51 52 53 54

    // 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
梦境迷离 已提交
55
    println(B3(1, 2, None, None))
56
  }
梦境迷离's avatar
梦境迷离 已提交
57 58
  "apply2" should "failed on case class" in {
    """@apply @toString case class C3(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))(o: Int = 1)""" shouldNot compile
59
  }
梦境迷离's avatar
梦境迷离 已提交
60

61
  "apply3" should "ok with currying" in {
梦境迷离's avatar
梦境迷离 已提交
62 63 64
    """@apply @toString class C2(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))(o: Int = 1)""" should compile
    @apply
    @toString class C1(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L))(o: Int = 1)
梦境迷离's avatar
梦境迷离 已提交
65 66
    @apply
    @toString class B3(int: Int)(val j: Int)(var k: Option[String] = None)(t: Option[Long] = Some(1L))
梦境迷离's avatar
梦境迷离 已提交
67 68
    @apply
    @toString class B4(int: Int, a: Seq[Seq[String]])(val j: Int, b: Seq[String])(var k: Option[String] = None, c: Seq[Option[String]])(t: Option[Long] = Some(1L))
梦境迷离's avatar
梦境迷离 已提交
69
  }
70 71 72

  "apply4" should "ok with generic" in {
    @apply
梦境迷离's avatar
梦境迷离 已提交
73 74
    @toString class B3[T, U](int: T, yy: Int)(val j: U)
    println(B3(1, 2)(2))
75 76 77 78 79 80

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

  }
81
}