From b36f3f8095e297e6196bb1e7268af1864312d248 Mon Sep 17 00:00:00 2001 From: Andreas Stenius Date: Wed, 18 Jan 2017 11:43:52 +0100 Subject: [PATCH] core/doc: make sure name is usable as slug. --- lib/docs/core/doc.rb | 5 ++++- test/lib/docs/core/doc_test.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/docs/core/doc.rb b/lib/docs/core/doc.rb index de826959..5baa9973 100644 --- a/lib/docs/core/doc.rb +++ b/lib/docs/core/doc.rb @@ -48,7 +48,10 @@ module Docs end def slug - slug = @slug || name.try(:downcase) + slug = @slug || ( + raise "Slug must be set explicitly when name (#{name}) consists of anything else than [\\w\\.%]" if /[^\w\.%]/ =~ name + name.try(:downcase) + ) version? ? "#{slug}~#{version_slug}" : slug end diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index 8c3937e2..ebf44664 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -55,6 +55,18 @@ class DocsDocTest < MiniTest::Spec doc.version = '42' assert_equal 'foo~42', doc.slug end + + it "returns 'foobar' when #name has been set to 'FooBar'" do + doc.name = 'FooBar' + assert_equal 'foobar', doc.slug + end + + it "raises error when #name has unsluggable characters" do + assert_raises do + doc.name = 'Foo-Bar' + doc.slug + end + end end describe ".slug=" do