Browse Source

Add initial version of picolisp semver

tags/v0.5.0
Alex Williams 4 years ago
commit
833f3661da
Signed by: aw GPG Key ID: 19EE4AAA361A7E2C
8 changed files with 238 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +20
    -0
      LICENSE
  3. +23
    -0
      Makefile
  4. +91
    -0
      README.md
  5. +11
    -0
      module.l
  6. +48
    -0
      semver.l
  7. +8
    -0
      test.l
  8. +35
    -0
      test/test_semver.l

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
.lib/
.modules/

+ 20
- 0
LICENSE View File

@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 Alexander Williams, Unscramble <license@unscramble.jp>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 23
- 0
Makefile View File

@@ -0,0 +1,23 @@
# picolisp-semver Makefile

PIL_MODULE_DIR ?= .modules
REPO_PREFIX ?= https://github.com/aw

# Unit testing
TEST_REPO = $(REPO_PREFIX)/picolisp-unit.git
TEST_DIR = $(PIL_MODULE_DIR)/picolisp-unit/HEAD

# Generic
.PHONY: check run-tests clean

$(TEST_DIR):
mkdir -p $(TEST_DIR) && \
git clone $(TEST_REPO) $(TEST_DIR)

check: $(TEST_DIR) run-tests

run-tests:
./test.l

clean:
rm -rf $(TEST_DIR)

+ 91
- 0
README.md View File

@@ -0,0 +1,91 @@
# PicoLisp SemVer

[SemVer 2.0.0](http://semver.org) library for PicoLisp

This library can validate and compare **Major.Minor.Patch** versions (ex: `"1.0.0"`).
It ignores pre-release, build, metadata, and special extensions (ex: `.alpha.1`).

# Requirements

* PicoLisp 32-bit or 64-bit v16.12+

# Usage

* Include `semver.l` in your application: `(load "semver.l")`
* Use one of the 3 public functions listed below

| Function | Description | Returns | Example |
| :---- | :---- | :---- | :---- |
| `semver-format` | Formats a version string into a list of integers | List of integers | `(1 4 2)` |
| `semver-cmp` | Compares two lists of integers | List containing NIL, 0 or T | `(NIL 0 T)` |
| `semver-compare` | Compares two version strings | NIL, 0, or T | `NIL` |
| `semver-compare-slow` | Compares two version strings using the spaceship `<=>` | NIL, 0, or T | `T` |

Version comparison is always from left to right.

A brief explanation of the result obtained from `semver-compare`:

```
if left < right then return NIL # left is older
if left = right then return 0 # left and right are the same
if left > right then return T # left is newer
```

# Examples

### 1. Compare two version strings

```
(load "semver.l")

(semver-compare "1.4.0" "1.5.0")
-> NIL
(semver-compare "1.5.0" "1.5.0")
-> 0
(semver-compare "1.6.0" "1.5.0")
-> T
```

### 2. Format a version string into a list of integers

```
(load "semver.l")

(semver-format "1.4.0")
-> (1 4 0)
```

### 3. Compare two lists of integers

* The `(car)` corresponds to the `major`
* The `(cadr)` corresponds to the `minor`
* The `(caddr)` corresponds to the `patch`

```
(load "semver.l")

(semver-cmp (2 3 2) (1 4 2))
-> (T NIL 0)
(semver-cmp (1 5 0) (1 4 2))
-> (0 T NIL)
(semver-cmp (2 3 2) (1 4 2))
-> (T NIL 0)
```

# Testing

This library now comes with full [unit tests](https://github.com/aw/picolisp-unit). To run the tests, type:

make check

# Contributing

If you find any bugs or issues, please [create an issue](https://github.com/aw/picolisp-semver/issues/new).

If you want to improve this library, please make a pull-request.

# License

[MIT License](LICENSE)

Copyright (c) 2017 Alexander Williams, Unscramble <license@unscramble.jp>

+ 11
- 0
module.l View File

@@ -0,0 +1,11 @@
[de MODULE_INFO
("name" "picolisp-semver")
("version" "0.1.0")
("summary" "PicoLisp SemVer")
("source" "https://jidoteki.com")
("author" "Alexander Williams")
("license" "MIT")
("copyright" "(c) 2017 Alexander Williams, Unscramble <license@unscramble.jp>")
("install" "make")
("requires"
("picolisp-unit" "v1.0.0" "https://github.com/aw/picolisp-unit.git") ]

+ 48
- 0
semver.l View File

@@ -0,0 +1,48 @@
# semver.l
#
# The MIT License (MIT)
#
# Copyright (c) 2017 Alexander Williams, Unscramble <license@unscramble.jp>

(local MODULE_INFO <=> semver-cmp semver-format semver-compare semver-compare-slow)

(load (pack (car (file)) "module.l"))

### internal

# http://stackoverflow.com/a/827656
# a <=> b :=
# if a < b then return -1
# if a = b then return 0
# if a > b then return 1
[de <=> (A B)
(cond
((< A B) NIL)
((= A B) 0)
((> A B) T) ]

### public

[de semver-cmp (V1 V2)
(list
(<=> (car V1) (car V2))
(<=> (cadr V1) (cadr V2))
(<=> (caddr V1) (caddr V2)) ]

[de semver-format (Version)
(mapcar format (mapcan '((S) (split S "-")) (split (chop Version) "."))) ]

[de semver-compare (A B)
(use (V1 V2)
(setq V1 (semver-format A)
V2 (semver-format B) )

(cond
((and (= (car V1) (car V2)) (= (cadr V1) (cadr V2)) (= (caddr V1) (caddr V2))) 0)
((or (> (car V1) (car V2)) (> (cadr V1) (cadr V2)) (> (caddr V1) (caddr V2))) T) ]

[de semver-compare-slow (A B)
(let R (semver-cmp (semver-format A) (semver-format B))
(cond
((and (=0 (car R)) (=0 (cadr R)) (=0 (caddr R))) 0)
((and (car R) (cadr R) (caddr R)) T) ]

+ 8
- 0
test.l View File

@@ -0,0 +1,8 @@
#!/usr/bin/env pil

(load ".modules/picolisp-unit/HEAD/unit.l")

(chdir "test/"
(mapcar load (filter '((N) (sub? "test_" N)) (dir "."))) )

(report)

+ 35
- 0
test/test_semver.l View File

@@ -0,0 +1,35 @@
(load "../semver.l")

[de test-internal-cmp ()
(assert-nil (<=> 1 2) "Ensure (a < b = NIL)")
(assert-equal 0 (<=> 2 2) "Ensure (a = b = 0)")
(assert-t (<=> 3 2) "Ensure (a > b = T)") ]

[de test-semver-format ()
(assert-equal (1 4 2) (semver-format "1.4.2") "Convert a version string to a list")
(assert-equal (1 4 2 NIL) (semver-format "1.4.2-alpha1") "Drop special version extensions") ]

[de test-semver-cmp ()
(assert-equal (0 T NIL) (semver-cmp (1 4 0) (1 3 2)) "Compare two version lists") ]

[execute
(prinl "^J Testing SemVer^J")

'(test-internal-cmp)
'(test-semver-format)
'(test-semver-cmp)
'(assert-nil (semver-compare-slow "1.4.0" "1.4.2") "Is 1.4.0 < 1.4.2 ? (slow)")
'(assert-nil (semver-compare-slow "1.3.0" "1.4.2") "Is 1.3.0 < 1.4.2 ? (slow)")
'(assert-nil (semver-compare-slow "0.4.0" "1.4.2") "Is 0.4.0 < 1.4.2 ? (slow)")
'(assert-t (semver-compare-slow "1.4.3" "1.4.2") "Is 1.4.4 > 1.4.2 ? (slow)")
'(assert-t (semver-compare-slow "1.5.2" "1.4.2") "Is 1.5.2 > 1.4.2 ? (slow)")
'(assert-t (semver-compare-slow "2.4.2" "1.4.2") "Is 2.4.2 > 1.4.2 ? (slow)")
'(assert-equal 0 (semver-compare-slow "1.4.2" "1.4.2") "Is 1.4.2 = 1.4.2 ? (slow)")
'(assert-nil (semver-compare "1.4.0" "1.4.2") "Is 1.4.0 < 1.4.2 ?")
'(assert-nil (semver-compare "1.3.0" "1.4.2") "Is 1.3.0 < 1.4.2 ?")
'(assert-nil (semver-compare "0.4.0" "1.4.2") "Is 0.4.0 < 1.4.2 ?")
'(assert-t (semver-compare "1.4.3" "1.4.2") "Is 1.4.4 > 1.4.2 ?")
'(assert-t (semver-compare "1.5.2" "1.4.2") "Is 1.5.2 > 1.4.2 ?")
'(assert-t (semver-compare "2.4.2" "1.4.2") "Is 2.4.2 > 1.4.2 ?")
'(assert-equal 0 (semver-compare "1.4.2" "1.4.2") "Is 1.4.2 = 1.4.2 ?")
]

Loading…
Cancel
Save