Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package com.example.contacts
  2.  
  3. import org.junit.Assert.assertEquals
  4. import org.junit.Before
  5. import org.junit.Test
  6. import org.junit.runner.RunWith
  7. import org.robolectric.RobolectricTestRunner
  8. import java.util.*
  9.  
  10. @RunWith(RobolectricTestRunner::class)
  11. class SearchTest {
  12. private val contacts = arrayListOf(
  13. Contact("Test name", "+79213229220"), Contact("John Tyler", "+79516671380"),
  14. Contact("Matthew Lynch", "+89645382940"), Contact("Adam Magician", "+65902745208")
  15. )
  16.  
  17. @Test
  18. fun searchTest(){
  19. val searchRes = search("Ma")
  20. assertEquals(
  21. setOf(
  22. Contact("Matthew Lynch", "+89645382940"),
  23. Contact("Adam Magician", "+65902745208")
  24. ), searchRes.asSequence().toSet()
  25. )
  26. }
  27.  
  28.  
  29.  
  30. fun search(constraint : CharSequence) : MutableList<Contact>{
  31. val searchList = mutableListOf<Contact>()
  32. if (constraint.isEmpty()) {
  33. searchList.addAll(contacts)
  34. } else {
  35. val filterPattern: String = constraint.toString().toLowerCase(Locale.ENGLISH).trim()
  36. for (contact: Contact in contacts) {
  37. if (contact.name.toLowerCase(Locale.ENGLISH).contains(filterPattern) ||
  38. contact.phoneNumber.contains(filterPattern)) {
  39. searchList.add(contact)
  40. }
  41. }
  42. }
  43. return searchList
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement