Skip to content

Commit

Permalink
Add by type test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Jul 31, 2018
1 parent 4d6c93e commit f352d8d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
55 changes: 28 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# ResourcePoet
Like [JavaPoet](https://github.com/square/javapoet), but for Android XML Resources
Kotlin API for generating Android XML Resources

[![Build Status](https://travis-ci.org/Commit451/ResourcesPoet.svg?branch=master)](https://travis-ci.org/Commit451/ResourcesPoet)
[![](https://jitpack.io/v/Commit451/ResourcesPoet.svg)](https://jitpack.io/#Commit451/ResourcesPoet)

# Gradle
## Gradle
```groovy
repositories {
jcenter()
...
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.Commit451:ResourcesPoet:latest.release.here'
implementation 'com.github.Commit451:ResourcesPoet:latest.release.here'
}
```

# Basic Usage
## Basic Usage
Write variables to the poet like:
```kotlin
val poet = ResourcesPoet.create()
.addString("app_name", "Test")
.addColor("color_primary", "#FF0000")
.addBool("is_cool", true)
.addComment("This is a comment")
.addDrawable("logo", "@drawable/logo")
.addStyle("AppTheme.Dark", "Base.AppTheme.Dark")
//etc
.addString("app_name", "Test")
.addColor("color_primary", "#FF0000")
.addBool("is_cool", true)
.addComment("This is a comment")
.addDrawable("logo", "@drawable/logo")
.addStyle("AppTheme.Dark", "Base.AppTheme.Dark")
//etc
```
When you are ready for the XML result as a file:
```kotlin
Expand Down Expand Up @@ -53,26 +54,26 @@ val poet = ResourcesPoet.create(file)
Most [resource types](https://developer.android.com/guide/topics/resources/available-resources.html) are supported. All look similar in usage:
```kotlin
val poet = ResourcesPoet.create()
.addBool("is_cool", true)
.addColor("color_primary", "#FF0000")
.addComment("This is a comment")
.addDimension("margin", "2dp")
.addDrawable("logo", "@drawable/logo")
.addId("some_id")
.addInteger("number", 0)
.addIntegerArray("numbers", numbers)
.addPlurals("songs", plurals)
.addString("app_name", "Test")
.addStringArray("stuff", strings)
.addStyle("AppTheme.Dark", "Base.AppTheme.Dark")
.addTypedArray("some_typed_array", typedArray)
.addBool("is_cool", true)
.addColor("color_primary", "#FF0000")
.addComment("This is a comment")
.addDimension("margin", "2dp")
.addDrawable("logo", "@drawable/logo")
.addId("some_id")
.addInteger("number", 0)
.addIntegerArray("numbers", numbers)
.addPlurals("songs", plurals)
.addString("app_name", "Test")
.addStringArray("stuff", strings)
.addStyle("AppTheme.Dark", "Base.AppTheme.Dark")
.addTypedArray("some_typed_array", typedArray)
```
We do not allow configuration of more complicated resources like `drawable` and `anim` in the creation sense. Maybe one day.
We do not allow configuration of more complicated resources like `drawable` and `anim` in the creation sense.

License
--------

Copyright 2017 Commit 451
Copyright 2018 Commit 451

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
40 changes: 40 additions & 0 deletions src/test/kotlin/com/commit451/resourcespoet/AddByTypeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.commit451.resourcespoet

import org.junit.Assert
import org.junit.Test

/**
* Tests the resources creation
*/
class AddByTypeTest {

@Test
fun addStringType() {
val poet = ResourcesPoet.create()
.add(Type.STRING, "app_name", "Test")

TestUtil.assertEquals("string.xml", poet)
}

@Test
fun addIntegerType() {
val poet = ResourcesPoet.create()
.add(Type.INTEGER, "number", "0")

TestUtil.assertEquals("integer.xml", poet)
}

@Test
fun addUnsupportedType() {
var exception: Exception? = null
try {
ResourcesPoet.create()
.add(Type.INTEGER_ARRAY, "fail", "fail")
} catch (e: Exception) {
exception = e
}

Assert.assertNotNull(exception)
Assert.assertTrue(exception is IllegalArgumentException)
}
}

0 comments on commit f352d8d

Please sign in to comment.