Skip to content

Instantly share code, notes, and snippets.

View ngallazzi's full-sized avatar
🎓
Learning by repetition

Nicola Gallazzi ngallazzi

🎓
Learning by repetition
View GitHub Profile
@ngallazzi
ngallazzi / compose_test_rules.kt
Created December 29, 2023 14:27
Compose test rules
class MyComposeTest {
@get:Rule
val composeTestRule = createComposeRule()
val composeTestRuleWithActivity = createAndroidComposeRule<YourActivity>()
}
@ngallazzi
ngallazzi / compose_ui_rule.kt
Created December 29, 2023 12:03
Compose UI Rule
class MyLayoutTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testMyLayout() {
// Start the app
composeTestRule.setContent {
MyLayout()
}
@ngallazzi
ngallazzi / espresso_view_matching.kt
Last active January 4, 2024 08:56
Espresso view matching
<EditText android:id="@+id/name_field"
android:layout_width="wrap_content"
android:hint="Please enter your name" android:layout_height="wrap_content" />
<Button android:id="@+id/greet_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Click me to greet!" />
<Text android:id="@+id/greet_field" android:layout_width="wrap_content"
android:visibility="gone"
fun enrichInvoice(invoice: Invoice, author: String, timeStamp: LocalDateTime): Invoice {
return invoice.copy(printingAuthor = author, printTimeStamp = timeStamp)
}
fun main() {
var invoice = Utils.getRandomInvoice(this, resources.configuration.locale)
invoice = controller.enrichInvoice(invoice,"Nicola", LocalDateTime.now())
}
fun addAuthor(invoice: Invoice, author: String): Invoice {
return invoice.copy(printingAuthor = author)
}
fun addPrintTimeStamp(invoice: Invoice, timeStamp: LocalDateTime): Invoice {
return invoice.copy(printTimeStamp = timeStamp)
}
fun main() {
var invoice = Utils.getRandomInvoice(this, resources.configuration.locale)
// Both functions print something
private fun printInvoiceHeader(header: InvoiceHeader) {
binding.tvInvoicePreview.text = header.toString()
}
private fun printCustomerDetails(customer: Customer) {
var customerDetails = ""
customerDetails += "CUSTOMER\n"
customerDetails += "First Name: ${customer.firstName}\nLast Name: ${customer.lastName}\n"
binding.tvInvoicePreview.append("$customerDetails\n")
@ngallazzi
ngallazzi / introduce_parameter_object_end.kt
Created February 4, 2022 14:11
Mastering refactoring - Introduce parameter object, final code
private fun printInvoiceHeader(header: InvoiceHeader) {
binding.tvInvoicePreview.text = header.toString()
}
data class InvoiceHeader(private val number: Int, private val date: LocalDate) {
override fun toString(): String {
return "INVOICE N° ${number}, ${formatter.format(date)}\n\n"
}
companion object {
@ngallazzi
ngallazzi / introduce_parameter_object_start.kt
Created February 4, 2022 14:08
Mastering refactoring - Introduce parameter object, starting code
private fun printInvoiceHeader(invoiceNumber: Int, invoiceDate: LocalDate, format: DateTimeFormatter) {
val invoiceHeader =
"INVOICE N° ${invoiceNumber}, ${invoiceDate.format(format)}\n\n"
binding.tvInvoicePreview.text = invoiceHeader
}
@ngallazzi
ngallazzi / change_function_declaration_end.kt
Last active February 4, 2022 13:45
Mastering refactoring - Change function declaration, final code
private fun printInvoiceSheet(invoice: Invoice) {
printInvoiceHeader(invoiceNumber = invoice.number, invoiceDate = invoice.date)
printInvoiceItemsSection(invoice.items, 22.0)
}
private fun printInvoiceHeader(invoiceNumber: Int, invoiceDate: LocalDate) {
val invoiceHeader = "INVOICE N° ${invoiceNumber}, ${invoiceDate}\n\n"
binding.tvInvoicePreview.text = invoiceHeader
}
@ngallazzi
ngallazzi / change_function_declaration_start.kt
Last active February 4, 2022 13:41
Mastering refactoring - change function declaration, start code
private fun printInvoiceSheet(invoice: Invoice) {
printDetails(invoice)
printInvoiceItemsSection(invoice.items, 22.0)
}
private fun printDetails(invoice: Invoice) {
val invoiceHeader = "INVOICE N° ${invoice.number}, ${invoice.date}\n\n"
binding.tvInvoicePreview.text = invoiceHeader
}