miga-base 0.7.22.0 → 0.7.25.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/README.md +1 -1
- data/Rakefile +1 -0
- data/lib/miga/cli/action/add.rb +10 -8
- data/lib/miga/cli/action/classify_wf.rb +12 -11
- data/lib/miga/cli/action/derep_wf.rb +3 -9
- data/lib/miga/cli/action/edit.rb +0 -1
- data/lib/miga/cli/action/find.rb +1 -1
- data/lib/miga/cli/action/generic.rb +1 -1
- data/lib/miga/cli/action/get.rb +7 -2
- data/lib/miga/cli/action/get_db.rb +16 -21
- data/lib/miga/cli/action/index_wf.rb +4 -2
- data/lib/miga/cli/action/init.rb +93 -144
- data/lib/miga/cli/action/init/daemon_helper.rb +1 -2
- data/lib/miga/cli/action/init/files_helper.rb +119 -0
- data/lib/miga/cli/action/ncbi_get.rb +1 -1
- data/lib/miga/cli/action/new.rb +15 -9
- data/lib/miga/cli/action/option.rb +44 -0
- data/lib/miga/cli/action/preproc_wf.rb +7 -5
- data/lib/miga/cli/action/quality_wf.rb +3 -3
- data/lib/miga/cli/action/tax_dist.rb +1 -1
- data/lib/miga/cli/action/tax_test.rb +1 -1
- data/lib/miga/cli/action/wf.rb +71 -53
- data/lib/miga/cli/base.rb +17 -5
- data/lib/miga/cli/objects_helper.rb +23 -18
- data/lib/miga/common.rb +4 -2
- data/lib/miga/common/net.rb +74 -0
- data/lib/miga/common/with_option.rb +83 -0
- data/lib/miga/common/with_result.rb +3 -2
- data/lib/miga/dataset/base.rb +20 -2
- data/lib/miga/dataset/result.rb +5 -3
- data/lib/miga/metadata.rb +25 -13
- data/lib/miga/project/base.rb +82 -2
- data/lib/miga/project/result.rb +4 -4
- data/lib/miga/remote_dataset.rb +2 -0
- data/lib/miga/result/stats.rb +2 -2
- data/lib/miga/version.rb +4 -2
- data/scripts/essential_genes.bash +18 -3
- data/scripts/miga.bash +8 -2
- data/scripts/mytaxa.bash +6 -5
- data/scripts/mytaxa_scan.bash +8 -7
- data/scripts/ogs.bash +2 -3
- data/scripts/ssu.bash +16 -2
- data/test/dataset_test.rb +5 -5
- data/test/lair_test.rb +1 -2
- data/test/net_test.rb +34 -0
- data/test/with_option_test.rb +115 -0
- data/utils/FastAAI/00.Libraries/01.SCG_HMMs/Archaea_SCG.hmm +41964 -0
- data/utils/FastAAI/00.Libraries/01.SCG_HMMs/Bacteria_SCG.hmm +32439 -0
- data/utils/FastAAI/00.Libraries/01.SCG_HMMs/Complete_SCG_DB.hmm +62056 -0
- data/utils/FastAAI/FastAAI/FastAAI +1336 -0
- data/utils/FastAAI/README.md +84 -0
- data/utils/FastAAI/kAAI_v1.0_virus.py +1296 -0
- data/utils/cleanup-databases.rb +2 -3
- data/utils/distance/base.rb +9 -0
- data/utils/distance/commands.rb +183 -81
- data/utils/distance/database.rb +69 -10
- data/utils/distance/pipeline.rb +15 -21
- data/utils/distance/runner.rb +27 -49
- data/utils/distance/temporal.rb +4 -2
- data/utils/distances.rb +2 -2
- data/utils/index_metadata.rb +1 -2
- data/utils/requirements.txt +6 -5
- data/utils/subclade/runner.rb +10 -11
- metadata +18 -6
data/lib/miga/project/result.rb
CHANGED
@@ -31,9 +31,9 @@ module MiGA::Project::Result
|
|
31
31
|
##
|
32
32
|
# Is this +task+ to be bypassed?
|
33
33
|
def ignore_task?(task)
|
34
|
-
metadata["run_#{task}"] == false
|
35
|
-
|
36
|
-
|
34
|
+
return true if metadata["run_#{task}"] == false
|
35
|
+
|
36
|
+
!clade? && @@INCLADE_TASKS.include?(task) && metadata["run_#{task}"] != true
|
37
37
|
end
|
38
38
|
|
39
39
|
##
|
@@ -74,7 +74,7 @@ module MiGA::Project::Result
|
|
74
74
|
return r
|
75
75
|
end
|
76
76
|
return nil unless result_files_exist?(base, %w[.proposed-clades])
|
77
|
-
unless
|
77
|
+
unless clade? ||
|
78
78
|
result_files_exist?(
|
79
79
|
base, %w[.pdf .classif .medoids .class.tsv .class.nwk]
|
80
80
|
)
|
data/lib/miga/remote_dataset.rb
CHANGED
@@ -162,6 +162,8 @@ class MiGA::RemoteDataset < MiGA::MiGA
|
|
162
162
|
txt.empty? ? sleep(1) : break
|
163
163
|
end
|
164
164
|
doc = MiGA::Json.parse(txt, symbolize: false, contents: true)
|
165
|
+
return if doc.nil? || doc['result'].nil? || doc['result'].empty?
|
166
|
+
|
165
167
|
@_ncbi_asm_json_doc = doc['result'][ doc['result']['uids'].first ]
|
166
168
|
end
|
167
169
|
|
data/lib/miga/result/stats.rb
CHANGED
@@ -118,7 +118,7 @@ module MiGA::Result::Stats
|
|
118
118
|
|
119
119
|
def compute_stats_essential_genes
|
120
120
|
stats = {}
|
121
|
-
if source.
|
121
|
+
if source.multi?
|
122
122
|
stats = { median_copies: 0, mean_copies: 0 }
|
123
123
|
File.open(file_path(:report), 'r') do |fh|
|
124
124
|
fh.each_line do |ln|
|
@@ -151,7 +151,7 @@ module MiGA::Result::Stats
|
|
151
151
|
source.save
|
152
152
|
|
153
153
|
# Inactivate low-quality datasets
|
154
|
-
min_qual =
|
154
|
+
min_qual = project.option(:min_qual)
|
155
155
|
if min_qual != 'no' && stats[:quality] < min_qual
|
156
156
|
source.inactivate! 'Low quality genome'
|
157
157
|
end
|
data/lib/miga/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
|
3
5
|
##
|
@@ -8,7 +10,7 @@ module MiGA
|
|
8
10
|
# - Float representing the major.minor version.
|
9
11
|
# - Integer representing gem releases of the current version.
|
10
12
|
# - Integer representing minor changes that require new version number.
|
11
|
-
VERSION = [0.7,
|
13
|
+
VERSION = [0.7, 25, 0].freeze
|
12
14
|
|
13
15
|
##
|
14
16
|
# Nickname for the current major.minor version.
|
@@ -16,7 +18,7 @@ module MiGA
|
|
16
18
|
|
17
19
|
##
|
18
20
|
# Date of the current gem release.
|
19
|
-
VERSION_DATE = Date.new(2021,
|
21
|
+
VERSION_DATE = Date.new(2021, 2, 22)
|
20
22
|
|
21
23
|
##
|
22
24
|
# Reference of MiGA.
|
@@ -24,18 +24,33 @@ fi
|
|
24
24
|
mkdir "${DATASET}.ess"
|
25
25
|
TYPE=$(miga ls -P "$PROJECT" -D "$DATASET" \
|
26
26
|
--metadata "type" | awk '{print $2}')
|
27
|
-
COLL=$(miga
|
28
|
-
[[ "$COLL" == "?" ]] && COLL=dupont_2012
|
27
|
+
COLL=$(miga option -P "$PROJECT" --key ess_coll)
|
29
28
|
if [[ "$TYPE" == "metagenome" || "$TYPE" == "virome" ]] ; then
|
30
29
|
FLAGS="--metagenome"
|
31
30
|
else
|
32
|
-
FLAGS="
|
31
|
+
FLAGS=""
|
33
32
|
fi
|
34
33
|
HMM.essential.rb \
|
35
34
|
-i "$FAA" -o "${DATASET}.ess.faa" -m "${DATASET}.ess/" \
|
36
35
|
-t "$CORES" -r "$DATASET" --collection "$COLL" $FLAGS \
|
37
36
|
> "${DATASET}.ess/log"
|
38
37
|
|
38
|
+
# Index for FastAAI
|
39
|
+
NOMULTI=$(miga list_datasets -P "$PROJECT" -D "$DATASET" --no-multi \
|
40
|
+
| wc -l | awk '{print $1}')
|
41
|
+
if [[ "$NOMULTI" -eq "1" ]] ; then
|
42
|
+
if [[ "$FAA" == *.gz ]] ; then
|
43
|
+
gzip -cd "$FAA" > "${DATASET}.faix"
|
44
|
+
else
|
45
|
+
cp "$FAA" "${DATASET}.faix"
|
46
|
+
fi
|
47
|
+
FastAAI --qp "${DATASET}.faix" --output "${DATASET}.faix" \
|
48
|
+
--ext ".faix" --index --input-paths --all-vs-all --threads "$CORES"
|
49
|
+
rm "${DATASET}.faix"
|
50
|
+
rm "${DATASET}.faix.hmm"
|
51
|
+
rm "${DATASET}.faix.hmm.filt"
|
52
|
+
fi
|
53
|
+
|
39
54
|
# Reduce files
|
40
55
|
if exists "$DATASET".ess/*.faa ; then
|
41
56
|
( cd "${DATASET}.ess" \
|
data/scripts/miga.bash
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
#!/bin/bash
|
2
|
+
|
3
|
+
# Setup environment
|
2
4
|
set -e
|
3
|
-
#MIGA=${MIGA:-$(cd "$(dirname "$0")/.."; pwd)}
|
4
5
|
MIGA_HOME=${MIGA_HOME:-"$HOME"}
|
6
|
+
SCRIPT=${SCRIPT:-$(basename "$0" .bash)}
|
5
7
|
# shellcheck source=/dev/null
|
6
8
|
. "$MIGA_HOME/.miga_rc"
|
9
|
+
|
10
|
+
# Ensure submodules are first in PATH
|
7
11
|
export PATH="$MIGA/bin:$MIGA/utils/enveomics/Scripts:$PATH"
|
8
|
-
|
12
|
+
export PATH="$MIGA/utils/FastAAI/FastAAI:$PATH"
|
9
13
|
|
14
|
+
# Ancillary functions
|
10
15
|
function exists { [[ -e "$1" ]] ; }
|
11
16
|
function fx_exists { [[ $(type -t "$1") == "function" ]] ; }
|
12
17
|
function miga_start_project_step {
|
@@ -28,6 +33,7 @@ function miga_end_project_step {
|
|
28
33
|
miga add_result -P "$PROJECT" -r "$SCRIPT" -f
|
29
34
|
}
|
30
35
|
|
36
|
+
# Environment header
|
31
37
|
if [[ "$SCRIPT" != "d" && "$SCRIPT" != "p" ]] ; then
|
32
38
|
echo ""
|
33
39
|
echo "######[ $SCRIPT ]######"
|
data/scripts/mytaxa.bash
CHANGED
@@ -14,15 +14,16 @@ if [[ "$MIGA_MYTAXA" == "no" ]] ; then
|
|
14
14
|
echo "This system doesn't currently support MyTaxa." \
|
15
15
|
> "$DATASET.nomytaxa.txt"
|
16
16
|
else
|
17
|
-
MT=$(dirname -- "$(which MyTaxa)")
|
18
|
-
|
19
17
|
# Check type of dataset
|
20
18
|
MULTI=$(miga list_datasets -P "$PROJECT" -D "$DATASET" --multi \
|
21
19
|
| wc -l | awk '{print $1}')
|
22
20
|
if [[ "$MULTI" -eq "1" ]] ; then
|
23
21
|
# Check requirements
|
24
|
-
|
25
|
-
|
22
|
+
MT=$(dirname -- "$(which MyTaxa)")
|
23
|
+
DB="$MIGA_HOME/.miga_db/AllGenomes.faa.dmnd"
|
24
|
+
[[ -e "$DB" ]] || DB="$MT/AllGenomes.faa.dmnd"
|
25
|
+
if [[ ! -e "$DB" ]] ; then
|
26
|
+
echo "Cannot locate the database: AllGenomes.faa.dmnd:" \
|
26
27
|
"no such file or directory" >&2
|
27
28
|
exit 1
|
28
29
|
fi
|
@@ -40,7 +41,7 @@ else
|
|
40
41
|
# Execute search
|
41
42
|
FAA="../../../06.cds/$DATASET.faa"
|
42
43
|
[[ -s "$FAA" ]] || FAA="${FAA}.gz"
|
43
|
-
diamond blastp -q "$FAA" -d "$
|
44
|
+
diamond blastp -q "$FAA" -d "$DB" \
|
44
45
|
-a "$DATASET.daa" -k 5 -p "$CORES" --min-score 60
|
45
46
|
diamond view -a "$DATASET.daa" -o "$DATASET.blast"
|
46
47
|
|
data/scripts/mytaxa_scan.bash
CHANGED
@@ -5,7 +5,6 @@ SCRIPT="mytaxa_scan"
|
|
5
5
|
# shellcheck source=scripts/miga.bash
|
6
6
|
. "$MIGA/scripts/miga.bash" || exit 1
|
7
7
|
DIR="$PROJECT/data/07.annotation/03.qa/02.mytaxa_scan"
|
8
|
-
[[ -d "$DIR" ]] || mkdir -p "$DIR"
|
9
8
|
cd "$DIR"
|
10
9
|
|
11
10
|
# Initialize
|
@@ -14,17 +13,16 @@ if [[ "$MIGA_MYTAXA" == "no" ]] ; then
|
|
14
13
|
echo "This system doesn't currently support MyTaxa." \
|
15
14
|
> "$DATASET.nomytaxa.txt"
|
16
15
|
else
|
17
|
-
MT=$(dirname -- "$(which MyTaxa)")
|
18
|
-
TMPDIR=$(mktemp -d /tmp/MiGA.XXXXXXXXXXXX)
|
19
|
-
trap "rm -rf '$TMPDIR'; exit" SIGHUP SIGINT SIGTERM
|
20
|
-
|
21
16
|
# Check type of dataset
|
22
17
|
NOMULTI=$(miga list_datasets -P "$PROJECT" -D "$DATASET" --no-multi \
|
23
18
|
| wc -l | awk '{print $1}')
|
24
19
|
if [[ "$NOMULTI" -eq "1" ]] ; then
|
25
20
|
# Check requirements
|
21
|
+
MT=$(dirname -- "$(which MyTaxa)")
|
22
|
+
DB="$MIGA_HOME/.miga_db/AllGenomes.faa.dmnd"
|
23
|
+
[[ -e "$DB" ]] || DB="$MT/AllGenomes.faa.dmnd"
|
26
24
|
if [[ ! -e "$MT/AllGenomes.faa.dmnd" ]] ; then
|
27
|
-
echo "Cannot locate the database:
|
25
|
+
echo "Cannot locate the database: AllGenomes.faa.dmnd:" \
|
28
26
|
"no such file or directory" >&2
|
29
27
|
exit 1
|
30
28
|
fi
|
@@ -39,13 +37,16 @@ else
|
|
39
37
|
exit 1
|
40
38
|
fi
|
41
39
|
|
40
|
+
TMPDIR=$(mktemp -d /tmp/MiGA.XXXXXXXXXXXX)
|
41
|
+
trap "rm -rf '$TMPDIR'; exit" SIGHUP SIGINT SIGTERM
|
42
|
+
|
42
43
|
FAA="../../../06.cds/$DATASET.faa"
|
43
44
|
[[ -s "$FAA" ]] || FAA="${FAA}.gz"
|
44
45
|
if [[ ! -s "$DATASET.mytaxa" ]] ; then
|
45
46
|
# Execute search
|
46
47
|
if [[ ! -s "$DATASET.blast" ]] ; then
|
47
48
|
diamond blastp -q "$FAA" -a "$DATASET.daa" -t "$TMPDIR" \
|
48
|
-
-d "$
|
49
|
+
-d "$DB" -k 5 -p "$CORES" --min-score 60
|
49
50
|
diamond view -a "$DATASET.daa" -o "$DATASET.blast" -t "$TMPDIR"
|
50
51
|
fi
|
51
52
|
|
data/scripts/ogs.bash
CHANGED
@@ -12,8 +12,7 @@ miga_start_project_step "$DIR"
|
|
12
12
|
DS=$(miga ls -P "$PROJECT" --ref --no-multi)
|
13
13
|
|
14
14
|
if [[ -n $DS ]] ; then
|
15
|
-
MIN_ID=$(miga
|
16
|
-
[[ $MIN_ID == "?" ]] && MIN_ID=80
|
15
|
+
MIN_ID=$(miga option -P "$PROJECT" --key ogs_identity)
|
17
16
|
if [[ ! -s miga-project.ogs ]] ; then
|
18
17
|
# Extract RBMs
|
19
18
|
if [[ ! -s miga-project.abc ]] ; then
|
@@ -34,7 +33,7 @@ if [[ -n $DS ]] ; then
|
|
34
33
|
|
35
34
|
# Estimate OGs and Clean RBMs
|
36
35
|
ogs.mcl.rb -o miga-project.ogs --abc miga-project.abc -t "$CORES"
|
37
|
-
if [[ $(miga
|
36
|
+
if [[ $(miga option -P "$PROJECT" --key clean_ogs) == "false" ]] ; then
|
38
37
|
gzip -9 miga-project.abc
|
39
38
|
else
|
40
39
|
rm miga-project.abc
|
data/scripts/ssu.bash
CHANGED
@@ -16,6 +16,7 @@ if [[ -s $fa ]] ; then
|
|
16
16
|
# Run barrnap
|
17
17
|
barrnap --quiet --threads "$CORES" "$fa" | grep "^##gff\\|;product=16S " \
|
18
18
|
> "$DATASET.ssu.gff"
|
19
|
+
|
19
20
|
# Extract
|
20
21
|
bedtools getfasta -s "-fi" "$fa" -bed "$DATASET.ssu.gff" \
|
21
22
|
-fo "$DATASET.ssu.all.fa"
|
@@ -24,9 +25,22 @@ if [[ -s $fa ]] ; then
|
|
24
25
|
FastA.filter.pl "$DATASET.ssu.fa.id" "$DATASET.ssu.all.fa" > "$DATASET.ssu.fa"
|
25
26
|
rm "$DATASET.ssu.fa.id"
|
26
27
|
[[ -e "$fa.fai" ]] && rm "$fa.fai"
|
28
|
+
|
29
|
+
# RDP classifier
|
30
|
+
if [[ "$MIGA_RDP" == "yes" && -s "$DATASET.ssu.all.fa" ]] ; then
|
31
|
+
java -jar "$MIGA_HOME/.miga_db/classifier.jar" classify \
|
32
|
+
-c 0.8 -f fixrank -g 16srrna -o "$DATASET.rdp.tsv" \
|
33
|
+
"$DATASET.ssu.all.fa"
|
34
|
+
echo "# Version: $(perl -pe 's/.*://' \
|
35
|
+
< "$MIGA_HOME/.miga_db/classifier.version.txt" \
|
36
|
+
| grep . | paste - - | perl -pe 's/\t/; /')" \
|
37
|
+
>> "$DATASET.rdp.tsv"
|
38
|
+
fi
|
39
|
+
|
27
40
|
# Gzip
|
28
|
-
|
29
|
-
|
41
|
+
for x in ssu.gff ssu.all.fa rdp.tsv ; do
|
42
|
+
[[ -e "${DATASET}.${x}" ]] && gzip -9 -f "${DATASET}.${x}"
|
43
|
+
done
|
30
44
|
fi
|
31
45
|
|
32
46
|
# Finalize
|
data/test/dataset_test.rb
CHANGED
@@ -29,7 +29,7 @@ class DatasetTest < Test::Unit::TestCase
|
|
29
29
|
assert_raise { MiGA::Dataset.new(project, 'dataset-1') }
|
30
30
|
assert_equal(project, dataset.project)
|
31
31
|
assert_equal('dataset0', dataset.name)
|
32
|
-
assert_predicate(dataset, :
|
32
|
+
assert_predicate(dataset, :ref?)
|
33
33
|
assert_equal(MiGA::Metadata, dataset.metadata.class)
|
34
34
|
assert_equal(:incomplete, dataset.status)
|
35
35
|
end
|
@@ -38,14 +38,14 @@ class DatasetTest < Test::Unit::TestCase
|
|
38
38
|
d2 = project.add_dataset('ds_save')
|
39
39
|
assert_respond_to(d2, :save)
|
40
40
|
d2.save
|
41
|
-
assert_not_predicate(d2, :
|
42
|
-
assert_not_predicate(d2, :
|
41
|
+
assert_not_predicate(d2, :multi?)
|
42
|
+
assert_not_predicate(d2, :nonmulti?)
|
43
43
|
assert_nil(d2.metadata[:type])
|
44
44
|
d2.metadata[:type] = :metagenome
|
45
45
|
d2.save
|
46
46
|
assert_equal(:metagenome, d2.metadata[:type])
|
47
|
-
assert_predicate(d2, :
|
48
|
-
assert_not_predicate(d2, :
|
47
|
+
assert_predicate(d2, :multi?)
|
48
|
+
assert_not_predicate(d2, :nonmulti?)
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_remove
|
data/test/lair_test.rb
CHANGED
@@ -75,10 +75,9 @@ class LairTest < Test::Unit::TestCase
|
|
75
75
|
assert_predicate(d, :active?)
|
76
76
|
end
|
77
77
|
|
78
|
-
out = capture_stderr { lair.terminate_daemons }.string
|
78
|
+
out = capture_stderr { lair.terminate_daemons ; sleep(2) }.string
|
79
79
|
assert_match(/Probing MiGA::Daemon/, out)
|
80
80
|
assert_match(/Sending termination message/, out)
|
81
|
-
sleep(2)
|
82
81
|
assert_not_predicate(d, :active?)
|
83
82
|
|
84
83
|
out = capture_stderr { assert { lair.daemon_loop } }.string
|
data/test/net_test.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FormatTest < Test::Unit::TestCase
|
4
|
+
include TestHelper
|
5
|
+
|
6
|
+
def test_known_hosts
|
7
|
+
m = MiGA::MiGA
|
8
|
+
assert_not_nil(m.known_hosts(:miga_db))
|
9
|
+
assert_not_nil(m.known_hosts('miga_db'))
|
10
|
+
assert_not_nil(m.known_hosts(:miga_dist))
|
11
|
+
assert_raise { m.known_kosts(:not_a_host) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_remote_connection
|
15
|
+
declare_remote_access
|
16
|
+
m = MiGA::MiGA
|
17
|
+
assert_raise { m.remote_connection(:bad_descriptor) }
|
18
|
+
assert_raise { m.remote_connection('http://microbial-genomes.org/') }
|
19
|
+
c = m.remote_connection(:miga_db)
|
20
|
+
assert_equal(Net::FTP, c.class)
|
21
|
+
c.close
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_download_file_ftp
|
25
|
+
declare_remote_access
|
26
|
+
m = MiGA::MiGA
|
27
|
+
f = tmpfile('t/test.txt')
|
28
|
+
d = File.dirname(f)
|
29
|
+
assert(!Dir.exist?(d))
|
30
|
+
m.download_file_ftp(:miga_online_ftp, 'test.txt', f)
|
31
|
+
assert(Dir.exist?(d))
|
32
|
+
assert_equal('miga', File.read(f).chomp)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'miga/common/with_option'
|
5
|
+
|
6
|
+
class WithDaemonTest < Test::Unit::TestCase
|
7
|
+
include TestHelper
|
8
|
+
|
9
|
+
class TestWithOption < MiGA::MiGA
|
10
|
+
include MiGA::Common::WithOption
|
11
|
+
|
12
|
+
attr_reader :metadata, :saved
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@metadata = { range: 0.9 }
|
16
|
+
@saved = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.OPTIONS
|
20
|
+
{
|
21
|
+
empty: {},
|
22
|
+
float: { type: Float },
|
23
|
+
range: { default: 1.0, in: -5.5..5.5, type: Float },
|
24
|
+
default: { default: 9, type: Integer },
|
25
|
+
token: { type: Integer, tokens: %w[yes no 0] },
|
26
|
+
proc: { default: proc { Date.today } },
|
27
|
+
bool: { in: [true, false] }
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def save
|
32
|
+
@saved = true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_with_option
|
37
|
+
o = TestWithOption.new
|
38
|
+
assert_respond_to(o, :option)
|
39
|
+
assert_equal(1, o.metadata.size)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_option
|
43
|
+
o = TestWithOption.new
|
44
|
+
assert_equal(9, o.option(:default))
|
45
|
+
assert_nil(o.option(:bool))
|
46
|
+
assert_raise { o.option(:not_an_option) }
|
47
|
+
assert_nil(o.option(:empty))
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_set_bool
|
51
|
+
o = TestWithOption.new
|
52
|
+
assert_nil(o.option(:bool))
|
53
|
+
assert(!o.saved)
|
54
|
+
assert_raise { o.set_option(:bool, 'true') }
|
55
|
+
assert_nil(o.option(:bool))
|
56
|
+
assert(!o.saved)
|
57
|
+
assert_equal(true, o.set_option(:bool, 'true', true))
|
58
|
+
assert(o.saved)
|
59
|
+
assert_equal(false, o.set_option(:bool, false))
|
60
|
+
assert_equal(false, o.set_option(:bool, 'false', true))
|
61
|
+
assert_nil(o.set_option(:bool, nil))
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_set_empty
|
65
|
+
o = TestWithOption.new
|
66
|
+
assert_nil(o.option(:empty))
|
67
|
+
assert_equal('a', o.set_option(:empty, 'a'))
|
68
|
+
assert_equal('1', o.set_option(:empty, '1', true))
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_all_options
|
72
|
+
o = TestWithOption.new
|
73
|
+
assert(o.all_options.is_a?(Hash))
|
74
|
+
assert_include(o.all_options.keys, :bool)
|
75
|
+
assert_nil(o.all_options[:bool])
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_option?
|
79
|
+
o = TestWithOption.new
|
80
|
+
assert(o.option?(:range))
|
81
|
+
assert(!o.option?(:not_an_option))
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_option_metadata
|
85
|
+
o = TestWithOption.new
|
86
|
+
assert_equal(0.9, o.option(:range))
|
87
|
+
assert_equal(1.0, o.set_option(:range, nil))
|
88
|
+
assert_equal(2.0, o.set_option(:range, 2.0))
|
89
|
+
assert_equal(3.0, o.set_option(:range, '3', true))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_option_range
|
93
|
+
o = TestWithOption.new
|
94
|
+
assert_raise { o.set_option(:range, 9.0) }
|
95
|
+
assert_raise { o.set_option(:range, 3) }
|
96
|
+
assert_raise { o.set_option(:range, true) }
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_option_proc
|
100
|
+
o = TestWithOption.new
|
101
|
+
assert(o.option(:proc).is_a?(Date))
|
102
|
+
assert(o.set_option(:proc, 1).is_a?(Integer))
|
103
|
+
assert(o.set_option(:proc, nil).is_a?(Date))
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_token
|
107
|
+
o = TestWithOption.new
|
108
|
+
assert_nil(o.option(:token))
|
109
|
+
assert_equal(1, o.set_option(:token, 1))
|
110
|
+
assert_equal(-2, o.set_option(:token, '-2', true))
|
111
|
+
assert_equal('yes', o.set_option(:token, 'yes'))
|
112
|
+
assert_equal('0', o.set_option(:token, '0', true))
|
113
|
+
assert_raise { o.set_option(:token, 'maybe') }
|
114
|
+
end
|
115
|
+
end
|