Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.contacts
- import org.junit.Assert.assertEquals
- import org.junit.Before
- import org.junit.Test
- import org.junit.runner.RunWith
- import org.robolectric.RobolectricTestRunner
- import java.util.*
- @RunWith(RobolectricTestRunner::class)
- class SearchTest {
- private val contacts = arrayListOf(
- Contact("Test name", "+79213229220"), Contact("John Tyler", "+79516671380"),
- Contact("Matthew Lynch", "+89645382940"), Contact("Adam Magician", "+65902745208")
- )
- @Test
- fun searchTest(){
- val searchRes = search("Ma")
- assertEquals(
- setOf(
- Contact("Matthew Lynch", "+89645382940"),
- Contact("Adam Magician", "+65902745208")
- ), searchRes.asSequence().toSet()
- )
- }
- fun search(constraint : CharSequence) : MutableList<Contact>{
- val searchList = mutableListOf<Contact>()
- if (constraint.isEmpty()) {
- searchList.addAll(contacts)
- } else {
- val filterPattern: String = constraint.toString().toLowerCase(Locale.ENGLISH).trim()
- for (contact: Contact in contacts) {
- if (contact.name.toLowerCase(Locale.ENGLISH).contains(filterPattern) ||
- contact.phoneNumber.contains(filterPattern)) {
- searchList.add(contact)
- }
- }
- }
- return searchList
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement