kotlin 测试authenticad视图模型更新

HomeViewModelTests
    @Test
    fun `When The Local Data Contains Already Seen Data By The User Than This Item Does Not Get Updated By Pull To Refresh`() {
        val evaluationList = createEvaluationList(Evaluation.Type.MidYear)
        val noteList = createNotesList()
        val omission = createOmission(true).toOmissionRealm()

        val evaluationsSubject = PublishSubject.create<List<Evaluation>>()
        val noteSubject = PublishSubject.create<List<Note>>()
        val omissionSubject = PublishSubject.create<List<Omission>>()

        _when(evaluationRepository.getEvaluations(profile)).thenReturn(evaluationsSubject)
        _when(notesRepository.getNotes(profile)).thenReturn(noteSubject)
        _when(omissionRepository.getOmissionsForProfile(profile)).thenReturn(omissionSubject)

        _when(evaluationRepository.fetchEvaluations(profile)).thenReturn(just(emptyList()))
        _when(notesRepository.fetchNotes(profile)).thenReturn(just(emptyList()))
        _when(omissionRepository.fetchOmissions(profile)).thenReturn(just(emptyList()))

        withSut {
            simulateLogin()
            omission.readByUser = true

            evaluationsSubject.onNext(evaluationList)
            noteSubject.onNext(noteList)
            omissionSubject.onNext(listOf(omission).map { it.toOmission() })

            omission.readByUser = false

            evaluationsSubject.onNext(evaluationList)
            noteSubject.onNext(noteList)
            omissionSubject.onNext(listOf(omission).map { it.toOmission() })

            items.value?.forEach {
                if (it is ListItem.DashboardItem.OmissionItem) {
                    Assert.assertEquals(it.readByUser, true)
                }
            }
        }
    }

kotlin Kotlin类和对象

Kotlin类和对象

visiblity_modifier.kt
// index.kt
package thiha

private val API_LINK = "http://something.org/test"
val PROJECT_NAME = "Test Kotlin"
internal val yummy = "Yummy"

fun main(args: Array<String>) {
    val animal = Animal("Puppy")
    println(API_LINK)
    animal.accessPublic()
    val test = Test()
    test.accessPrivate()
}

class Test() {
    fun accessPrivate() {
        println(API_LINK)
    }
}

// Animal.kt
package thiha

class Animal(val name: String) {
    fun accessPublic() {
        println(PROJECT_NAME)
        println(yummy)
    }
}

kotlin Kotlin类和对象

Kotlin类和对象

interfaces_2.kt
fun main(args: Array<String>) {
    val developer = Developer("John")
    developer.eat()
    developer.sleep()
    developer.code()
    developer.repeat()
}

interface HumanInterface {
    fun eat() {
        print(" EAT ")
    }
    fun sleep() {
        print(" SLEEP ")
    }
}

interface CoderInterface {
    fun eat() {
        print(" EAT ")
    }
    fun sleep() {
        print(" SLEEP ")
    }
    fun code() {
        print(" CODE ")
    }
    fun repeat() {
        print(" REPEAT ")
    }
}

class Developer(val name: String): HumanInterface, CoderInterface {
    override fun eat() {
        super<HumanInterface>.eat()
        super<CoderInterface>.eat()
        print(" DEVELOER ")
    }

    override fun sleep() {
        super<HumanInterface>.sleep()
        super<CoderInterface>.sleep()
        print(" DEVELOPER ")

    }

    override fun code() {
        super.code()
        print(" DEVELOPER ")
    }

    override fun repeat() {
        super.repeat()
        print(" DEVELOPER ")
    }
}

kotlin Kotlin类和对象

Kotlin类和对象

interfaces.kt
fun main(args: Array<String>) {
    val dog = Dog("Aung Net")
    dog.eat()
    dog.sleep()
    dog.bite()
    dog.cute()
}
interface AnimalInterface {
    var name: String
    fun eat()
    fun sleep()
}
interface CuteInterface {
    fun cute()
}
interface DogInterface : AnimalInterface {
    fun bite()
}
class Dog(override var name: String) : DogInterface, CuteInterface {
    override fun cute() {
        println("So Cute")
    }
    override fun eat() {
        println("eat")
    }
    override fun sleep() {
        println("sleep")
    }
    override fun bite() {
        println("bite")
    }
}

kotlin Getters和Setters示例

Get用于返回显示的值<br/> Set用于设置要分配的值的条件

getset.kt
fun main(args: Array<String>) {

    val maria = Girl()
    maria.actualAge = 15
    maria.age = 15
    println("Maria: actual age = ${maria.actualAge}")
    println("Maria: pretended age = ${maria.age}")

    val angela = Girl()
    angela.actualAge = 35
    angela.age = 35
    println("Angela: actual age = ${angela.actualAge}")
    println("Angela: pretended age = ${angela.age}")
}

class Girl {
    var age: Int = 0
    get() = field
    set(value) {
        field = if (value < 18)
            18
        else if (value >= 18 && value <= 30)
            value
        else
            value-3
    }

    var actualAge: Int = 0
}

kotlin 字符串到字符串转换

chartostring.kt
var x = readLine()!!.toCharArray()
print (x.joinToString("").toUpperCase())

kotlin

class.kt
//  Create a package for your project under src
// In that package add a new kotlin class file and a main method file
// In class file:
class aqua
{
    val width = 10
    val height = 20
    val breadth = 30
    
    fun volume(): Int = width*height*length/1000
            
}

// In main, create an object

val myaqua = aqua()

// To access elements

print(myaqua.width)

kotlin Lambda表达式

Lambdas是没有名字的函数

lambda.kt
{println("Hello") }()           // O/P : Hello.     Executed immediately

val swim = { println("swim") }()        // Lambda Declaration
swim()                                  // Lambda Call

//Passing Arguments

val half = {number: Int -> number / 2 }

// Using Return Types

val half: (Int) -> Int = (number -> number/2)           // Takes an Int and returns an Int. Number is returned

kotlin 制图

map.kt
// Hashes unique value to the elements

  val myMap = mapOf<Int,String>(1 to "YG", 4 to "AS", 3 to "VD")  
    for(key in myMap.keys)  
        {
            println(myMap[key])         // Prints Elements
            println(key)                // Prints Keys
            println("Element at key $key = ${myMap.get(key)}")
        }
        
// Example: Mapping Symptoms to Disease

val cures = mapOf("fever"to"malaria","itching"to"ringworm")

println(cures.get("fever"))     //returns "malaria"
    OR
println(cures["fever"])
    
// Setting Defaults

cures.getOrDefault("fever","unidentified")

cures.getOrElse("fever") {/*Code to be executed as default*/}

// Editable Mapping

val cures = mutableMapOf("fever"to"malaria","itching"to"ringworm")
cures.put("pimples"to"chickenpox")
cures.remove("pimples")


kotlin 过滤器

filter.kt
val objects = listOf ("rock", "paper", "scissors", "alligator", "flowers")

// Condition = The first letter of each element will be checked for p 

println(ojects.filter {it[0] == 'p'} )                 

// Returns the elements of the list that match the condition in the brackets
// O/P: [paper]


// Returns elements that start with c and end with e

val spices = listOf("curre", "pepper", "cayenne", "ginger", "ced curre", "green curry", "red pepper" )
    val sort = spices.filter{it[0] == 'c' && it[it.length.dec()] == 'e'}
    print (sort)
    

// Searches for keyword curry and returns list sorted by string length

val sorted = spices.filter { it.contains("curry") }.sortedBy { it.length }

// Filtering the first 3 items by 'c'

val first3 = spices.take(3).filter{it.startsWith('c')}